-
-
Notifications
You must be signed in to change notification settings - Fork 47k
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
cclauss
merged 29 commits into
TheAlgorithms:master
from
kosuri-indu:add/trapped_water_problem
Oct 7, 2023
Merged
Changes from all 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 7ebd539
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] aad0efb
to make changes for error : B006
kosuri-indu 04c87a7
Merge branch 'add/trapped_water_problem' of https://github.com/kosuri…
kosuri-indu 95ff91c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b18f44a
to make changes for error : B006
kosuri-indu d514b2e
to make changes for error : B006
kosuri-indu ddf7b99
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6962866
to make changes in doctest
kosuri-indu acaf9a7
Merge branch 'add/trapped_water_problem' of https://github.com/kosuri…
kosuri-indu dadd1a9
to make changes in doctest
kosuri-indu 42021c4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d61cbea
Update dynamic_programming/trapped_water.py
kosuri-indu 3f9951d
Update dynamic_programming/trapped_water.py
kosuri-indu 03984af
to make changes in parameters
kosuri-indu 3634905
Merge branch 'add/trapped_water_problem' of https://github.com/kosuri…
kosuri-indu eaa775e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bf94d38
to make changes in parameters
kosuri-indu d980935
to make changes in parameters
kosuri-indu 1ca6701
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 10609bc
Update dynamic_programming/trapped_water.py
kosuri-indu a0854dd
to make changes in parameters
kosuri-indu 7f9f1f1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d684771
for negative heights
kosuri-indu 9230589
Update dynamic_programming/trapped_water.py
kosuri-indu 20b78d5
to remove falsy
kosuri-indu 473a87d
Merge branch 'add/trapped_water_problem' of https://github.com/kosuri…
kosuri-indu f17ff76
Final edits
cclauss 1bb35ee
tuple[int, ...]
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
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(heights: tuple[int, ...]) -> 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 | ||
>>> trapped_rainwater((7, 1, 5, 3, 6, -1)) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: No height can be negative | ||
""" | ||
if not heights: | ||
return 0 | ||
if any(h < 0 for h in heights): | ||
raise ValueError("No height can be negative") | ||
length = len(heights) | ||
|
||
left_max = [0] * length | ||
left_max[0] = heights[0] | ||
for i, height in enumerate(heights[1:], start=1): | ||
left_max[i] = max(height, left_max[i - 1]) | ||
|
||
right_max = [0] * length | ||
right_max[-1] = heights[-1] | ||
for i in range(length - 2, -1, -1): | ||
right_max[i] = max(heights[i], right_max[i + 1]) | ||
|
||
return sum( | ||
min(left, right) - height | ||
for left, right, height in zip(left_max, right_max, heights) | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
print(f"{trapped_rainwater((0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1)) = }") | ||
print(f"{trapped_rainwater((7, 1, 5, 3, 6, 4)) = }") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.