-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Implement Trapped RainWater #2716
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
realstealthninja
merged 11 commits into
TheAlgorithms:master
from
sozelfist:dp/trapped_rainwater
Aug 31, 2024
+104
−0
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a977bad
chore: add `trapped_rainwater.cpp` to DIRECTORY.md
sozelfist 5c4c54d
feat: implement Trapped Rain Water algorithm
sozelfist b8891e2
chore: add links to the trapped rain water problem
sozelfist 2f1beb4
chore(docs): remove Trapped Rain Water dir
sozelfist 4995696
ref: add edges tests
sozelfist 8cbdfdb
Merge branch 'master' into dp/trapped_rainwater
sozelfist d8bf45b
doc: adding Sozel as author
realstealthninja 5be0b58
doc: includes documentatino
realstealthninja 05f5062
ref: use `unsigned int` for height of walls
sozelfist d6b091e
fix: use fixed-width integers instead of unsigned int
realstealthninja 04b6513
chore: rearrange included libraries
sozelfist 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,101 @@ | ||
/** | ||
* @file | ||
* @brief Implementation of the [Trapped Rainwater | ||
* Problem](https://www.geeksforgeeks.org/trapping-rain-water/) | ||
* @details | ||
* This implementation calculates the amount of rainwater that can be trapped | ||
* between walls represented by an array of heights. | ||
*/ | ||
|
||
#include <algorithm> /// For std::min and std::max | ||
#include <cassert> /// For assert | ||
#include <vector> /// For vector container | ||
realstealthninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/* | ||
* @namespace | ||
* @brief Dynamic Programming Algorithms | ||
*/ | ||
namespace dynamic_programming { | ||
/** | ||
* @brief Function to calculate the trapped rainwater | ||
* @param heights Array representing the heights of walls | ||
* @return The amount of trapped rainwater | ||
*/ | ||
int trappedRainwater(const std::vector<int>& heights) { | ||
sozelfist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int n = heights.size(); | ||
if (n <= 2) | ||
return 0; // No water can be trapped with less than 3 walls | ||
|
||
std::vector<int> leftMax(n), rightMax(n); | ||
|
||
// Calculate the maximum height of wall to the left of each wall | ||
leftMax[0] = heights[0]; | ||
for (int i = 1; i < n; ++i) { | ||
leftMax[i] = std::max(leftMax[i - 1], heights[i]); | ||
} | ||
|
||
// Calculate the maximum height of wall to the right of each wall | ||
rightMax[n - 1] = heights[n - 1]; | ||
for (int i = n - 2; i >= 0; --i) { | ||
rightMax[i] = std::max(rightMax[i + 1], heights[i]); | ||
} | ||
|
||
// Calculate the trapped rainwater between walls | ||
int trappedWater = 0; | ||
for (int i = 0; i < n; ++i) { | ||
trappedWater += | ||
std::max(0, std::min(leftMax[i], rightMax[i]) - heights[i]); | ||
} | ||
|
||
return trappedWater; | ||
} | ||
|
||
} // namespace dynamic_programming | ||
|
||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
static void test() { | ||
std::vector<int> test_basic = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}; | ||
assert(dynamic_programming::trappedRainwater(test_basic) == 6); | ||
|
||
std::vector<int> test_peak_under_water = {3, 0, 2, 0, 4}; | ||
assert(dynamic_programming::trappedRainwater(test_peak_under_water) == 7); | ||
|
||
std::vector<int> test_bucket = {5, 1, 5}; | ||
assert(dynamic_programming::trappedRainwater(test_bucket) == 4); | ||
|
||
std::vector<int> test_skewed_bucket = {4, 1, 5}; | ||
assert(dynamic_programming::trappedRainwater(test_skewed_bucket) == 3); | ||
|
||
std::vector<int> test_empty = {}; | ||
assert(dynamic_programming::trappedRainwater(test_empty) == 0); | ||
|
||
std::vector<int> test_flat = {0, 0, 0, 0, 0}; | ||
assert(dynamic_programming::trappedRainwater(test_flat) == 0); | ||
|
||
std::vector<int> test_no_trapped_water = {1, 1, 2, 4, 0, 0, 0}; | ||
assert(dynamic_programming::trappedRainwater(test_no_trapped_water) == 0); | ||
|
||
std::vector<int> test_single_elevation = {5}; | ||
assert(dynamic_programming::trappedRainwater(test_single_elevation) == 0); | ||
|
||
std::vector<int> test_two_point_elevation = {5, 1}; | ||
assert(dynamic_programming::trappedRainwater(test_two_point_elevation) == | ||
0); | ||
|
||
std::vector<int> test_large_elevation_map_difference = {5, 1, 6, 1, | ||
7, 1, 8}; | ||
assert(dynamic_programming::trappedRainwater( | ||
test_large_elevation_map_difference) == 15); | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
test(); // run self-test implementations | ||
return 0; | ||
} |
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.