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 9ba695b commit 85f449cCopy full SHA for 85f449c
42-trapping-rain-water/trapping-rain-water.py
@@ -0,0 +1,24 @@
1
+class Solution:
2
+ def trap(self, height: List[int]) -> int:
3
+ if not height:
4
+ return 0
5
+
6
+ water_blocks = 0
7
+ start_index = 0
8
+ end_index = len(height) - 1
9
10
+ while start_index < end_index:
11
+ if height[start_index] <= height[end_index]:
12
+ min_height = height[start_index]
13
+ start_index += 1
14
+ while start_index < end_index and height[start_index] < min_height:
15
+ water_blocks += min_height - height[start_index]
16
17
+ else:
18
+ min_height = height[end_index]
19
+ end_index -= 1
20
+ while start_index < end_index and height[end_index] < min_height:
21
+ water_blocks += min_height - height[end_index]
22
23
24
+ return water_blocks
0 commit comments