Skip to content

Commit f426e30

Browse files
committed
Run Continue over trapping rain water
1 parent c69f2a4 commit f426e30

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/leetcode/hard/trapping-rain-water/trapping_rain_water.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,44 @@
2222

2323

2424
def trap(heights: List[int]) -> int:
25-
if isinstance(heights, list):
25+
"""
26+
Compute how much water can be trapped after raining.
27+
28+
Args:
29+
heights (List[int]): A list of non-negative integers representing an elevation map.
30+
31+
Returns:
32+
int: The amount of water that can be trapped.
33+
"""
34+
# Edge case: input is not a list
35+
if not isinstance(heights, list):
2636
raise TypeError("Arguments must be of type list")
37+
38+
# Edge case: empty list
2739
if len(heights) < 1:
2840
return 0
2941

42+
# Initialize variables to keep track of water trapped and maximum heights on both sides
3043
water_trapped = 0
3144
left = 0
3245
right = len(heights) - 1
3346
max_left = heights[left]
3447
max_right = heights[right]
3548

49+
# Loop through the list until the two pointers meet
3650
while left < right:
51+
# Move the pointer that points to the smaller maximum height
3752
if max_left <= max_right:
3853
left += 1
3954
else:
4055
right -= 1
4156

57+
# Update the trapped water and maximum heights for this iteration
4258
current_index = left if max_left <= max_right else right
4359
trapped = min(max_left, max_right) - heights[current_index]
4460
water_trapped += trapped if trapped > 0 else 0
4561
max_right = max(max_right, heights[right])
4662
max_left = max(max_left, heights[left])
4763

64+
# Return the total amount of water that can be trapped
4865
return water_trapped

0 commit comments

Comments
 (0)