-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
feat: implement optimized two-pointer approach for trapping rainwater (#2975) #2976
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
Open
kanavgoyal898
wants to merge
2
commits into
TheAlgorithms:master
Choose a base branch
from
kanavgoyal898:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,111 @@ | ||
/** | ||
* @file | ||
* @brief Implementation of the [Trapped Rainwater | ||
* Problem](https://www.geeksforgeeks.org/trapping-rain-water/) | ||
* @details | ||
* This implementation calculates the total trapped rainwater using a | ||
* two-pointer approach. It maintains two pointers (`left` and `right`) and | ||
* tracks the maximum height seen so far from both ends (`leftMax` and | ||
* `rightMax`). At each step, the algorithm decides which side to process based | ||
* on which boundary is smaller, ensuring O(n) time and O(1) space complexity. | ||
* @author [kanavgoyal898](https://github.com/kanavgoyal898) | ||
*/ | ||
|
||
#include <algorithm> /// For std::min and std::max | ||
#include <cassert> /// For assert | ||
#include <cstddef> /// For std::size_t | ||
#include <cstdint> | ||
#include <vector> /// For std::vector | ||
|
||
/* | ||
* @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 | ||
*/ | ||
uint32_t trappedRainwater(const std::vector<uint32_t>& heights) { | ||
std::size_t n = heights.size(); | ||
if (n <= 2) | ||
return 0; // Not enough walls to trap water | ||
|
||
std::size_t left = 0, right = n - 1; | ||
uint32_t leftMax = 0, rightMax = 0, trappedWater = 0; | ||
|
||
// Traverse from both ends towards the center | ||
while (left < right) { | ||
if (heights[left] < heights[right]) { | ||
// Water trapped depends on the tallest wall to the left | ||
if (heights[left] >= leftMax) | ||
leftMax = heights[left]; // Update left max | ||
else | ||
trappedWater += | ||
leftMax - heights[left]; // Water trapped at current left | ||
++left; | ||
} else { | ||
// Water trapped depends on the tallest wall to the right | ||
if (heights[right] >= rightMax) | ||
rightMax = heights[right]; // Update right max | ||
else | ||
trappedWater += | ||
rightMax - | ||
heights[right]; // Water trapped at current right | ||
--right; | ||
} | ||
} | ||
|
||
return trappedWater; | ||
} | ||
|
||
} // namespace dynamic_programming | ||
|
||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
static void test() { | ||
std::vector<uint32_t> test_basic = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}; | ||
assert(dynamic_programming::trappedRainwater(test_basic) == 6); | ||
|
||
std::vector<uint32_t> test_peak_under_water = {3, 0, 2, 0, 4}; | ||
assert(dynamic_programming::trappedRainwater(test_peak_under_water) == 7); | ||
|
||
std::vector<uint32_t> test_bucket = {5, 1, 5}; | ||
assert(dynamic_programming::trappedRainwater(test_bucket) == 4); | ||
|
||
std::vector<uint32_t> test_skewed_bucket = {4, 1, 5}; | ||
assert(dynamic_programming::trappedRainwater(test_skewed_bucket) == 3); | ||
|
||
std::vector<uint32_t> test_empty = {}; | ||
assert(dynamic_programming::trappedRainwater(test_empty) == 0); | ||
|
||
std::vector<uint32_t> test_flat = {0, 0, 0, 0, 0}; | ||
assert(dynamic_programming::trappedRainwater(test_flat) == 0); | ||
|
||
std::vector<uint32_t> test_no_trapped_water = {1, 1, 2, 4, 0, 0, 0}; | ||
assert(dynamic_programming::trappedRainwater(test_no_trapped_water) == 0); | ||
|
||
std::vector<uint32_t> test_single_elevation = {5}; | ||
assert(dynamic_programming::trappedRainwater(test_single_elevation) == 0); | ||
|
||
std::vector<uint32_t> test_two_point_elevation = {5, 1}; | ||
assert(dynamic_programming::trappedRainwater(test_two_point_elevation) == | ||
0); | ||
|
||
std::vector<uint32_t> 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; | ||
} |
Oops, something went wrong.
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.