Skip to content

Commit d126fd5

Browse files
Add Trapping Rainwater problem implementation (Two Pointer Approach) (#6990)
* Add Trapping Rainwater problem implementation (Two Pointer Approach) * Add Wikipedia reference link for Trapping Rainwater problem * fix: format TrappingRainwater.java for CI check * fix: add package and resolve checkstyle errors for TrappingRainwater.java * fix: declare TrappingRainwater as final to pass Checkstyle * Add test cases for TrappingRainwater algorithm * Add test cases for TrappingRainwater algorithm * Move TrappingRainwater algorithm to stacks package * Fix: Move TrappingRainwater to stacks and normalize newline in JugglerSequence * Fix: Normalize newline in JugglerSequence to ensure test consistency * Fix: Normalize newline in JugglerSequence and remove return statement * Add JaCoCo plugin for code coverage reporting * Revert pom.xml to original version from master * Fix: Revert the pom.xml file --------- Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
1 parent d28fee9 commit d126fd5

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,4 @@
155155
</plugin>
156156
</plugins>
157157
</build>
158-
</project>
158+
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.thealgorithms.stacks;
2+
/**
3+
* Trapping Rainwater Problem
4+
* Given an array of non-negative integers representing the height of bars,
5+
* compute how much water it can trap after raining.
6+
*
7+
* Example:
8+
* Input: [4,2,0,3,2,5]
9+
* Output: 9
10+
*
11+
* Time Complexity: O(n)
12+
* Space Complexity: O(1)
13+
*
14+
* Reference: https://en.wikipedia.org/wiki/Trapping_rain_water
15+
*/
16+
public final class TrappingRainwater {
17+
18+
private TrappingRainwater() {
19+
throw new UnsupportedOperationException("Utility class");
20+
}
21+
22+
public static int trap(int[] height) {
23+
int left = 0;
24+
int right = height.length - 1;
25+
int leftMax = 0;
26+
int rightMax = 0;
27+
int result = 0;
28+
29+
while (left < right) {
30+
if (height[left] < height[right]) {
31+
if (height[left] >= leftMax) {
32+
leftMax = height[left];
33+
} else {
34+
result += leftMax - height[left];
35+
}
36+
left++;
37+
} else {
38+
if (height[right] >= rightMax) {
39+
rightMax = height[right];
40+
} else {
41+
result += rightMax - height[right];
42+
}
43+
right--;
44+
}
45+
}
46+
return result;
47+
}
48+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.thealgorithms.stacks;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class TrappingRainwaterTest {
8+
9+
@Test
10+
public void testExampleCase() {
11+
int[] height = {4, 2, 0, 3, 2, 5};
12+
assertEquals(9, TrappingRainwater.trap(height));
13+
}
14+
15+
@Test
16+
public void testNoTrapping() {
17+
int[] height = {1, 2, 3, 4, 5};
18+
assertEquals(0, TrappingRainwater.trap(height));
19+
}
20+
21+
@Test
22+
public void testFlatSurface() {
23+
int[] height = {0, 0, 0, 0};
24+
assertEquals(0, TrappingRainwater.trap(height));
25+
}
26+
27+
@Test
28+
public void testSymmetricPit() {
29+
int[] height = {3, 0, 2, 0, 3};
30+
assertEquals(7, TrappingRainwater.trap(height));
31+
}
32+
33+
@Test
34+
public void testSingleBar() {
35+
int[] height = {5};
36+
assertEquals(0, TrappingRainwater.trap(height));
37+
}
38+
}

0 commit comments

Comments
 (0)