Files
LeetCode-Go/leetcode/0042.Trapping-Rain-Water/42. Trapping Rain Water.go
2020-08-07 17:06:53 +08:00

24 lines
450 B
Go

package leetcode
func trap(height []int) int {
res, left, right, maxLeft, maxRight := 0, 0, len(height)-1, 0, 0
for left <= right {
if height[left] <= height[right] {
if height[left] > maxLeft {
maxLeft = height[left]
} else {
res += maxLeft - height[left]
}
left++
} else {
if height[right] >= maxRight {
maxRight = height[right]
} else {
res += maxRight - height[right]
}
right--
}
}
return res
}