-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0042H. Trapping Rain Water.py
More file actions
26 lines (25 loc) · 919 Bytes
/
0042H. Trapping Rain Water.py
File metadata and controls
26 lines (25 loc) · 919 Bytes
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
#Runtime: 76 ms, faster than 27.82% of Python3 online submissions for Trapping Rain Water.
#Memory Usage: 15.4 MB, less than 29.47% of Python3 online submissions for Trapping Rain Water.
class Solution:
def trap(self, height: List[int]) -> int:
try:
while height[0] < height[1]:
height.pop(0)
while height[-1] < height[-2]:
height.pop()
except:
return 0
w = 0
while True:
max_i = height.index(max(height))
max_h = height.pop(max_i)
if not height or max(height) == 0:
return w
max2_i = height.index(max(height))
max2_h = max(height)
if max2_i >= max_i:
mi = max_i
else:
mi = max2_i + 1
for i in range(mi, max(max2_i, max_i)):
w += max2_h - height.pop(mi)