Skip to content

Commit f17ff76

Browse files
authored
Final edits
1 parent 473a87d commit f17ff76

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

Diff for: dynamic_programming/trapped_water.py

+28-26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
2-
Given an array of non-negative integers representing an elevation map where
3-
the width of each bar is 1,this program calculates how much rainwater can be trapped.
2+
Given an array of non-negative integers representing an elevation map where the width
3+
of each bar is 1, this program calculates how much rainwater can be trapped.
44
5-
Example - height = [0,1,0,2,1,0,1,3,2,1,2,1]
5+
Example - height = (0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1)
66
Output: 6
77
This problem can be solved using the concept of "DYNAMIC PROGRAMMING".
88
@@ -13,46 +13,48 @@
1313
"""
1414

1515

16-
def trapped_rainwater(height: list[int]) -> int:
16+
def trapped_rainwater(heights: tuple[int]) -> int:
1717
"""
18-
The trapped_rainwater function calculates the total amount of rainwater
19-
that can be trapped given an array of bar heights.
20-
It uses a dynamic programming approach, determining the maximum height of bars
21-
on both sides for each bar, and then computing the trapped water above each bar.
18+
The trapped_rainwater function calculates the total amount of rainwater that can be
19+
trapped given an array of bar heights.
20+
It uses a dynamic programming approach, determining the maximum height of bars on
21+
both sides for each bar, and then computing the trapped water above each bar.
2222
The function returns the total trapped water.
2323
24-
>>> trapped_rainwater([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])
24+
>>> trapped_rainwater((0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1))
2525
6
26-
>>> trapped_rainwater([7, 1, 5, 3, 6, 4])
26+
>>> trapped_rainwater((7, 1, 5, 3, 6, 4))
2727
9
28+
>>> trapped_rainwater((7, 1, 5, 3, 6, -1))
29+
Traceback (most recent call last):
30+
...
31+
ValueError: No height can be negative
2832
"""
29-
if not height:
33+
if not heights:
3034
return 0
31-
if any(h < 0 for h in height):
35+
if any(h < 0 for h in heights):
3236
raise ValueError("No height can be negative")
33-
length = len(height)
37+
length = len(heights)
3438

3539
left_max = [0] * length
36-
left_max[0] = height[0]
37-
for i in range(1, length):
38-
left_max[i] = max(height[i], left_max[i - 1])
40+
left_max[0] = heights[0]
41+
for i, height in enumerate(heights[1:], start=1):
42+
left_max[i] = max(height, left_max[i - 1])
3943

4044
right_max = [0] * length
41-
right_max[length - 1] = height[length - 1]
45+
right_max[-1] = heights[-1]
4246
for i in range(length - 2, -1, -1):
43-
right_max[i] = max(height[i], right_max[i + 1])
47+
right_max[i] = max(heights[i], right_max[i + 1])
4448

45-
trapped_water: int = 0
46-
47-
for i in range(length):
48-
water_level = min(left_max[i], right_max[i])
49-
trapped_water += water_level - height[i]
50-
51-
return trapped_water
49+
return sum(
50+
min(left, right) - height
51+
for left, right, height in zip(left_max, right_max, heights)
52+
)
5253

5354

5455
if __name__ == "__main__":
5556
import doctest
5657

5758
doctest.testmod()
58-
print(f"{trapped_rainwater([0,1,0,2,1,0,1,3,2,1,2,1])}")
59+
print(f"{trapped_rainwater((0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1)) = }")
60+
print(f"{trapped_rainwater((7, 1, 5, 3, 6, 4)) = }")

0 commit comments

Comments
 (0)