-
Notifications
You must be signed in to change notification settings - Fork 20.7k
Add Trapping Rainwater problem implementation (Two Pointer Approach) #6990
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
DenizAltunkapan
merged 16 commits into
TheAlgorithms:master
from
Arzoo1701:add-trapping-rainwater
Nov 5, 2025
+87
−1
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5a5bcd9
Add Trapping Rainwater problem implementation (Two Pointer Approach)
Arzoo1701 364bcd9
Add Wikipedia reference link for Trapping Rainwater problem
Arzoo1701 b9a0653
fix: format TrappingRainwater.java for CI check
Arzoo1701 6d42fda
fix: add package and resolve checkstyle errors for TrappingRainwater.…
Arzoo1701 3b569d6
fix: declare TrappingRainwater as final to pass Checkstyle
Arzoo1701 09bd367
Add test cases for TrappingRainwater algorithm
Arzoo1701 40b4297
Add test cases for TrappingRainwater algorithm
Arzoo1701 a611617
Move TrappingRainwater algorithm to stacks package
Arzoo1701 a48fe3f
Fix: Move TrappingRainwater to stacks and normalize newline in Juggle…
Arzoo1701 b959623
Fix: Normalize newline in JugglerSequence to ensure test consistency
Arzoo1701 4f9073d
Fix: Normalize newline in JugglerSequence and remove return statement
Arzoo1701 73963f3
Merge branch 'master' into add-trapping-rainwater
Arzoo1701 2989ac1
Add JaCoCo plugin for code coverage reporting
Arzoo1701 76ff418
Revert pom.xml to original version from master
Arzoo1701 1dc4bff
Fix: Revert the pom.xml file
Arzoo1701 d0ea66c
Merge branch 'master' into add-trapping-rainwater
DenizAltunkapan 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 |
|---|---|---|
|
|
@@ -155,4 +155,4 @@ | |
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> | ||
| </project> | ||
48 changes: 48 additions & 0 deletions
48
src/main/java/com/thealgorithms/stacks/TrappingRainwater.java
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,48 @@ | ||
| package com.thealgorithms.stacks; | ||
| /** | ||
| * Trapping Rainwater Problem | ||
| * Given an array of non-negative integers representing the height of bars, | ||
| * compute how much water it can trap after raining. | ||
| * | ||
| * Example: | ||
| * Input: [4,2,0,3,2,5] | ||
| * Output: 9 | ||
| * | ||
| * Time Complexity: O(n) | ||
| * Space Complexity: O(1) | ||
| * | ||
| * Reference: https://en.wikipedia.org/wiki/Trapping_rain_water | ||
| */ | ||
| public final class TrappingRainwater { | ||
|
|
||
| private TrappingRainwater() { | ||
| throw new UnsupportedOperationException("Utility class"); | ||
| } | ||
|
|
||
| public static int trap(int[] height) { | ||
| int left = 0; | ||
| int right = height.length - 1; | ||
| int leftMax = 0; | ||
| int rightMax = 0; | ||
| int result = 0; | ||
|
|
||
| while (left < right) { | ||
| if (height[left] < height[right]) { | ||
| if (height[left] >= leftMax) { | ||
| leftMax = height[left]; | ||
| } else { | ||
| result += leftMax - height[left]; | ||
| } | ||
| left++; | ||
| } else { | ||
| if (height[right] >= rightMax) { | ||
| rightMax = height[right]; | ||
| } else { | ||
| result += rightMax - height[right]; | ||
| } | ||
| right--; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
src/test/java/com/thealgorithms/stacks/TrappingRainwaterTest.java
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,38 @@ | ||
| package com.thealgorithms.stacks; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class TrappingRainwaterTest { | ||
|
|
||
| @Test | ||
| public void testExampleCase() { | ||
| int[] height = {4, 2, 0, 3, 2, 5}; | ||
| assertEquals(9, TrappingRainwater.trap(height)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNoTrapping() { | ||
| int[] height = {1, 2, 3, 4, 5}; | ||
| assertEquals(0, TrappingRainwater.trap(height)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFlatSurface() { | ||
| int[] height = {0, 0, 0, 0}; | ||
| assertEquals(0, TrappingRainwater.trap(height)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSymmetricPit() { | ||
| int[] height = {3, 0, 2, 0, 3}; | ||
| assertEquals(7, TrappingRainwater.trap(height)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSingleBar() { | ||
| int[] height = {5}; | ||
| assertEquals(0, TrappingRainwater.trap(height)); | ||
| } | ||
| } |
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.