diff --git a/src/main/java/com/thealgorithms/searches/TrappingRainWater.java b/src/main/java/com/thealgorithms/searches/TrappingRainWater.java new file mode 100644 index 000000000000..48d9391dab97 --- /dev/null +++ b/src/main/java/com/thealgorithms/searches/TrappingRainWater.java @@ -0,0 +1,53 @@ +package com.thealgorithms.searches; + +/** + * Trapping Rain Water + * + * Given n non-negative integers representing an elevation map where the width of + * each bar is 1, compute how much water it can trap after raining. + * + * Approach: Two-pointer optimized O(n) time and O(1) extra space. + */ +public final class TrappingRainWater { + + private TrappingRainWater() {} + + /** + * Compute trapped rain water amount for the given heights array. + * + * @param height array of non-negative integers + * @return total units of trapped water + * @throws IllegalArgumentException if height is null + */ + public static int trap(final int[] height) { + if (height == null) { + throw new IllegalArgumentException("height array must not be null"); + } + + int left = 0; + int right = height.length - 1; + int leftMax = 0; + int rightMax = 0; + int trapped = 0; + + while (left <= right) { + if (height[left] <= height[right]) { + if (height[left] >= leftMax) { + leftMax = height[left]; + } else { + trapped += leftMax - height[left]; + } + left++; + } else { + if (height[right] >= rightMax) { + rightMax = height[right]; + } else { + trapped += rightMax - height[right]; + } + right--; + } + } + + return trapped; + } +} diff --git a/src/main/java/com/thealgorithms/strings/Upper.java b/src/main/java/com/thealgorithms/strings/Upper.java index 5e248cb6ee39..14e6a4909ca8 100644 --- a/src/main/java/com/thealgorithms/strings/Upper.java +++ b/src/main/java/com/thealgorithms/strings/Upper.java @@ -19,21 +19,21 @@ public static void main(String[] args) { * * @param s the string to convert * @return the {@code String}, converted to uppercase. + * @throws IllegalArgumentException if {@code s} is null */ public static String toUpperCase(String s) { if (s == null) { - throw new IllegalArgumentException("Input string connot be null"); + throw new IllegalArgumentException("Input string cannot be null"); } if (s.isEmpty()) { return s; } - StringBuilder result = new StringBuilder(s); - for (int i = 0; i < result.length(); ++i) { - char currentChar = result.charAt(i); - if (Character.isLetter(currentChar) && Character.isLowerCase(currentChar)) { - result.setCharAt(i, Character.toUpperCase(currentChar)); + char[] chars = s.toCharArray(); + for (int i = 0; i < chars.length; i++) { + if (Character.isLowerCase(chars[i])) { + chars[i] = Character.toUpperCase(chars[i]); } } - return result.toString(); + return new String(chars); } } diff --git a/src/test/java/com/thealgorithms/searches/TrappingRainWaterTest.java b/src/test/java/com/thealgorithms/searches/TrappingRainWaterTest.java new file mode 100644 index 000000000000..6891b3499da7 --- /dev/null +++ b/src/test/java/com/thealgorithms/searches/TrappingRainWaterTest.java @@ -0,0 +1,38 @@ +package com.thealgorithms.searches; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +public class TrappingRainWaterTest { + + @Test + public void testExample() { + int[] height = {4, 2, 0, 3, 2, 5}; + assertEquals(9, TrappingRainWater.trap(height)); + } + + @Test + public void testEmpty() { + int[] height = {}; + assertEquals(0, TrappingRainWater.trap(height)); + } + + @Test + public void testNoTrapping() { + int[] height = {0, 1, 2, 3, 4}; + assertEquals(0, TrappingRainWater.trap(height)); + } + + @Test + public void testFlat() { + int[] height = {3, 3, 3, 3}; + assertEquals(0, TrappingRainWater.trap(height)); + } + + @Test + public void testNull() { + assertThrows(IllegalArgumentException.class, () -> TrappingRainWater.trap(null)); + } +}