-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrain_trap.go
34 lines (32 loc) · 959 Bytes
/
rain_trap.go
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
32
33
34
package problem0042
/*
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.
*/
func trap(height []int) int {
var res, left, right, mLeft, mRight int
right = len(height) - 1
for left <= right {
if height[left] <= height[right] {
// If the current left is the lower boundry of the pool
if height[left] >= mLeft {
// If left is a new max height, there's no water above it
mLeft = height[left]
} else {
// If it's not the new max height, it's covered in water
res += mLeft - height[left]
}
left++
} else {
// If the current right is the lower boundry of the pool
if height[right] >= mRight {
// If right is a new max height, there's no water above it
mRight = height[right]
} else {
// If it's not the new max height, it's covered in water
res += mRight - height[right]
}
right--
}
}
return res
}