Skip to content

add : trapped water program under dynamic programming #10027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Oct 7, 2023
Merged
Changes from 21 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
441cebc
to add the trapped water program
kosuri-indu Oct 7, 2023
7ebd539
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2023
aad0efb
to make changes for error : B006
kosuri-indu Oct 7, 2023
04c87a7
Merge branch 'add/trapped_water_problem' of https://github.com/kosuri…
kosuri-indu Oct 7, 2023
95ff91c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2023
b18f44a
to make changes for error : B006
kosuri-indu Oct 7, 2023
d514b2e
to make changes for error : B006
kosuri-indu Oct 7, 2023
ddf7b99
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2023
6962866
to make changes in doctest
kosuri-indu Oct 7, 2023
acaf9a7
Merge branch 'add/trapped_water_problem' of https://github.com/kosuri…
kosuri-indu Oct 7, 2023
dadd1a9
to make changes in doctest
kosuri-indu Oct 7, 2023
42021c4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2023
d61cbea
Update dynamic_programming/trapped_water.py
kosuri-indu Oct 7, 2023
3f9951d
Update dynamic_programming/trapped_water.py
kosuri-indu Oct 7, 2023
03984af
to make changes in parameters
kosuri-indu Oct 7, 2023
3634905
Merge branch 'add/trapped_water_problem' of https://github.com/kosuri…
kosuri-indu Oct 7, 2023
eaa775e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2023
bf94d38
to make changes in parameters
kosuri-indu Oct 7, 2023
d980935
to make changes in parameters
kosuri-indu Oct 7, 2023
1ca6701
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2023
10609bc
Update dynamic_programming/trapped_water.py
kosuri-indu Oct 7, 2023
a0854dd
to make changes in parameters
kosuri-indu Oct 7, 2023
7f9f1f1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2023
d684771
for negative heights
kosuri-indu Oct 7, 2023
9230589
Update dynamic_programming/trapped_water.py
kosuri-indu Oct 7, 2023
20b78d5
to remove falsy
kosuri-indu Oct 7, 2023
473a87d
Merge branch 'add/trapped_water_problem' of https://github.com/kosuri…
kosuri-indu Oct 7, 2023
f17ff76
Final edits
cclauss Oct 7, 2023
1bb35ee
tuple[int, ...]
cclauss Oct 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions dynamic_programming/trapped_water.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Given an array of non-negative integers representing an elevation map where
the width of each bar is 1,this program calculates how much rainwater can be trapped.

Example - height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
This problem can be solved using the concept of "DYNAMIC PROGRAMMING".

We calculate the maximum height of bars on the left and right of every bar in array.
Then iterate over the width of structure and at each index.
The amount of water that will be stored is equal to minimum of maximum height of bars
on both sides minus height of bar at current position.
"""


def trapped_rainwater(height: list[int] = None) -> int:
"""
The trapped_rainwater function calculates the total amount of rainwater
that can be trapped given an array of bar heights.
It uses a dynamic programming approach, determining the maximum height of bars
on both sides for each bar, and then computing the trapped water above each bar.
The function returns the total trapped water.

trapped_rainwater([0,1,0,2,1,0,1,3,2,1,2,1])
>>> 6
trapped_rainwater([7,1,5,3,6,4])
>>> 9
"""
if not height:
return 0
if any(height < 0):
raise ValueError("No height can be negative")
length = len(height)

left_max = [0] * length
left_max[0] = height[0]
for i in range(1, length):
left_max[i] = max(height[i], left_max[i - 1])

right_max = [0] * length
right_max[length - 1] = height[length - 1]
for i in range(length - 2, -1, -1):
right_max[i] = max(height[i], right_max[i + 1])

trapped_water: int = 0

for i in range(length):
water_level = min(left_max[i], right_max[i])
trapped_water += water_level - height[i]

return trapped_water


if __name__ == "__main__":
import doctest

doctest.testmod()
print(f"{trapped_rainwater([0,1,0,2,1,0,1,3,2,1,2,1])}")