|
1 | 1 | """
|
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. |
4 | 4 |
|
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) |
6 | 6 | Output: 6
|
7 | 7 | This problem can be solved using the concept of "DYNAMIC PROGRAMMING".
|
8 | 8 |
|
|
13 | 13 | """
|
14 | 14 |
|
15 | 15 |
|
16 |
| -def trapped_rainwater(height: list[int]) -> int: |
| 16 | +def trapped_rainwater(heights: tuple[int]) -> int: |
17 | 17 | """
|
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. |
22 | 22 | The function returns the total trapped water.
|
23 | 23 |
|
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)) |
25 | 25 | 6
|
26 |
| - >>> trapped_rainwater([7, 1, 5, 3, 6, 4]) |
| 26 | + >>> trapped_rainwater((7, 1, 5, 3, 6, 4)) |
27 | 27 | 9
|
| 28 | + >>> trapped_rainwater((7, 1, 5, 3, 6, -1)) |
| 29 | + Traceback (most recent call last): |
| 30 | + ... |
| 31 | + ValueError: No height can be negative |
28 | 32 | """
|
29 |
| - if not height: |
| 33 | + if not heights: |
30 | 34 | return 0
|
31 |
| - if any(h < 0 for h in height): |
| 35 | + if any(h < 0 for h in heights): |
32 | 36 | raise ValueError("No height can be negative")
|
33 |
| - length = len(height) |
| 37 | + length = len(heights) |
34 | 38 |
|
35 | 39 | 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]) |
39 | 43 |
|
40 | 44 | right_max = [0] * length
|
41 |
| - right_max[length - 1] = height[length - 1] |
| 45 | + right_max[-1] = heights[-1] |
42 | 46 | 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]) |
44 | 48 |
|
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 | + ) |
52 | 53 |
|
53 | 54 |
|
54 | 55 | if __name__ == "__main__":
|
55 | 56 | import doctest
|
56 | 57 |
|
57 | 58 | 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