Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/main/java/com/thealgorithms/searches/TrappingRainWater.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
14 changes: 7 additions & 7 deletions src/main/java/com/thealgorithms/strings/Upper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Loading