-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq42_Trapping_Rain_Water.py
75 lines (70 loc) · 2.18 KB
/
q42_Trapping_Rain_Water.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Solution:
def trap(self, height):
"""
:type height: List[int]
:rtype: int
"""
if len(height) < 3:
return 0
left = []
wid = []
ans = 0
for h in height:
if not left:
if h > 0:
left.append(h)
wid.append(1)
elif h < left[-1]:
left.append(h)
wid.append(1)
elif h == left[-1]:
wid[-1] += 1
else: # h>left[-1]
newwid = 1
while left and h > left[-1]:
mid = left.pop()
width = wid.pop()
if left:
lefth = left[-1]
if lefth < h:
wid[-1] += width
ans += (lefth - mid) * width
elif lefth > h:
newwid = 1 + width
ans += (h - mid) * width
else:
ans += (h - mid) * width
if left and left[-1]==h:
wid[-1]+=1+width
else:
left.append(h)
wid.append(newwid)
return ans
def trap2(self, height):
"""
:type height: List[int]
:rtype: int
"""
left = 0
right = len(height)-1
left_max = 0
right_max = 0
result = 0
while left<right:
if height[left] < height[right]:
if height[left] < left_max:
result += left_max-height[left]
left_max = max(left_max,height[left])
left+=1
else:
if height[right] < right_max:
result += right_max-height[right]
right_max = max(right_max,height[right])
right-=1
return result
height1 = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
height2 = [2,1,0,2]
height3 = [6,4,2,0,3,2,0,3,1,4,5,3,2,7,5,3,0,1,2,1,3,4,6,8,1,3]
print(Solution().trap(height1))
print(Solution().trap(height2))
print(Solution().trap(height3))