From d126fd59f7c4f9aac8de23db2766bc6f6121394d Mon Sep 17 00:00:00 2001 From: Arzoo1701 <122432635+Arzoo1701@users.noreply.github.com> Date: Wed, 5 Nov 2025 23:11:08 +0530 Subject: [PATCH] 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 --- pom.xml | 2 +- .../stacks/TrappingRainwater.java | 48 +++++++++++++++++++ .../stacks/TrappingRainwaterTest.java | 38 +++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/thealgorithms/stacks/TrappingRainwater.java create mode 100644 src/test/java/com/thealgorithms/stacks/TrappingRainwaterTest.java diff --git a/pom.xml b/pom.xml index 825d86f8b..7e9369672 100644 --- a/pom.xml +++ b/pom.xml @@ -155,4 +155,4 @@ - + \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/stacks/TrappingRainwater.java b/src/main/java/com/thealgorithms/stacks/TrappingRainwater.java new file mode 100644 index 000000000..072665061 --- /dev/null +++ b/src/main/java/com/thealgorithms/stacks/TrappingRainwater.java @@ -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; + } +} diff --git a/src/test/java/com/thealgorithms/stacks/TrappingRainwaterTest.java b/src/test/java/com/thealgorithms/stacks/TrappingRainwaterTest.java new file mode 100644 index 000000000..909be6cd4 --- /dev/null +++ b/src/test/java/com/thealgorithms/stacks/TrappingRainwaterTest.java @@ -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)); + } +}