forked from LeetCode-in-Scala/LeetCode-in-Scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.scala
31 lines (26 loc) · 974 Bytes
/
Solution.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package g0001_0100.s0042_trapping_rain_water
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming #Two_Pointers
// #Stack #Monotonic_Stack #Dynamic_Programming_I_Day_9 #Udemy_Two_Pointers
// #Big_O_Time_O(n)_Space_O(1) #2023_10_31_Time_521_ms_(87.88%)_Space_58_MB_(24.24%)
object Solution {
def trap(height: Array[Int]): Int = {
var left = 0
var right = height.length - 1
var result = 0
var lowerWall = 0
while (left < right) {
val leftValue = height(left)
val rightValue = height(right)
if (leftValue < rightValue) {
lowerWall = Math.max(leftValue, lowerWall)
result += lowerWall - leftValue
left += 1
} else {
lowerWall = Math.max(rightValue, lowerWall)
result += lowerWall - rightValue
right -= 1
}
}
result
}
}