Skip to content

Commit 85f449c

Browse files
committed
Time: 86 ms (98.73%) | Memory: 18.4 MB (74.82%) - LeetSync
1 parent 9ba695b commit 85f449c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
start_index += 1
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+
end_index -= 1
23+
24+
return water_blocks

0 commit comments

Comments
 (0)