We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 788d95b commit 99df0d2Copy full SHA for 99df0d2
data_structures/arrays/trapping_rain_water.py
@@ -0,0 +1,20 @@
1
+def trap(height):
2
+ if not height:
3
+ return 0
4
+ left, right = 0, len(height) - 1
5
+ max_left, max_right = 0, 0
6
+ water = 0
7
+ while left < right:
8
+ if height[left] < height[right]:
9
+ if height[left] >= max_left:
10
+ max_left = height[left]
11
+ else:
12
+ water += max_left - height[left]
13
+ left += 1
14
15
+ if height[right] >= max_right:
16
+ max_right = height[right]
17
18
+ water += max_right - height[right]
19
+ right -= 1
20
+ return water
0 commit comments