1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
class Solution:
def trap(self, height: List[int]) -> int:
n = len(height)
left_max_height = [0] * n
left_max_height[0] = height[0]
for i in range(1, n):
left_max_height[i] = max(left_max_height[i - 1], height[i])
right_max_height = [0] * n
right_max_height[n - 1] = height[n - 1]
for i in range(n - 2, -1, -1):
right_max_height[i] = max(right_max_height[i + 1], height[i])
rainfall = 0
for i in range(n):
rainfall += min(left_max_height[i], right_max_height[i]) - height[i]
return rainfall
|