Skip to content

Commit adcdd9a

Browse files
committed
Added 'trapping-rain-water' challenge
1 parent d0f67f6 commit adcdd9a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

trapping-rain-water/.solve.py.swp

12 KB
Binary file not shown.

trapping-rain-water/solve.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
class Solution:
3+
def trap(self,height:List[int]) -> int:
4+
ans=0
5+
if len(height) < 3:
6+
return 0
7+
left,right = 0,len(height) - 1
8+
left_max ,right_max= height[left],height[right]
9+
while (left < right):
10+
left_max = max(left_max,height[left])
11+
right_max = max(right_max,height[right])
12+
if left_max < right_max:
13+
ans+= left_max - height[left]
14+
left += 1
15+
else:
16+
ans+= right_max - height[right]
17+
right -= 1
18+
return ans
19+
if __name__ == "__main__":
20+
s = Solution()
21+
print(s.trap([4,2,0,3,2,5]))

0 commit comments

Comments
 (0)