-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42.py
More file actions
23 lines (18 loc) · 655 Bytes
/
42.py
File metadata and controls
23 lines (18 loc) · 655 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# leetcode problem no. 42(Hard )
class Solution:
def trap(self, height: List[int]) -> int:
if not height:
return 0
left, right = 0, len(height) - 1
leftMax, rightMax = height[left], height[right]
result = 0
while left < right:
if leftMax < rightMax:
left += 1
leftMax = max(leftMax, height[left])
result += leftMax - height[left]
else:
right -= 1
rightMax = max(rightMax, height[right])
result += rightMax - height[right]
return result