(height []int)
| 7 | } |
| 8 | |
| 9 | func trap(height []int) int { |
| 10 | left := 0 |
| 11 | right := len(height) - 1 |
| 12 | |
| 13 | ret := 0 |
| 14 | |
| 15 | leftMax := 0 |
| 16 | rightMax := 0 |
| 17 | for left < right { |
| 18 | if height[left] < height[right] { |
| 19 | if height[left] > leftMax { |
| 20 | leftMax = height[left] |
| 21 | } else { |
| 22 | ret += leftMax - height[left] |
| 23 | } |
| 24 | left++ |
| 25 | } else { |
| 26 | if height[right] > rightMax { |
| 27 | rightMax = height[right] |
| 28 | } else { |
| 29 | ret += rightMax - height[right] |
| 30 | } |
| 31 | right-- |
| 32 | } |
| 33 | } |
| 34 | return ret |
| 35 | } |