-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrapping-rain-water.py
More file actions
34 lines (21 loc) · 930 Bytes
/
Copy pathtrapping-rain-water.py
File metadata and controls
34 lines (21 loc) · 930 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
27
28
29
30
31
32
33
34
class Solution:
def trap(self, height: List[int]) -> int:
left = 0
left_pointer = left
right = len(height)-1
right_pointer = right
vol = 0
while left_pointer < right_pointer :
if height[left_pointer] <= height[right_pointer]:
left_pointer+=1
if height[left_pointer] <= height[left]:
vol+= abs(height[left_pointer]-height[left])
else:
left=left_pointer
else:
right_pointer-=1
if height[right_pointer] <= height[right]:
vol+= abs(height[right_pointer]-height[right])
else:
right=right_pointer
return vol