Skip to content

Commit b1c6cda

Browse files
corrected typpo and addeed optimized code
1 parent b424c76 commit b1c6cda

File tree

3 files changed

+92
-19
lines changed

3 files changed

+92
-19
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.searches;
2+
3+
/**
4+
* Trapping Rain Water
5+
*
6+
* Given n non-negative integers representing an elevation map where the width of
7+
* each bar is 1, compute how much water it can trap after raining.
8+
*
9+
* Approach: Two-pointer optimized O(n) time and O(1) extra space.
10+
*/
11+
public final class TrappingRainWater {
12+
13+
private TrappingRainWater() {}
14+
15+
/**
16+
* Compute trapped rain water amount for the given heights array.
17+
*
18+
* @param height array of non-negative integers
19+
* @return total units of trapped water
20+
* @throws IllegalArgumentException if height is null
21+
*/
22+
public static int trap(final int[] height) {
23+
if (height == null) {
24+
throw new IllegalArgumentException("height array must not be null");
25+
}
26+
27+
int left = 0;
28+
int right = height.length - 1;
29+
int leftMax = 0;
30+
int rightMax = 0;
31+
int trapped = 0;
32+
33+
while (left <= right) {
34+
if (height[left] <= height[right]) {
35+
if (height[left] >= leftMax) {
36+
leftMax = height[left];
37+
} else {
38+
trapped += leftMax - height[left];
39+
}
40+
left++;
41+
} else {
42+
if (height[right] >= rightMax) {
43+
rightMax = height[right];
44+
} else {
45+
trapped += rightMax - height[right];
46+
}
47+
right--;
48+
}
49+
}
50+
51+
return trapped;
52+
}
53+
}
Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.thealgorithms.strings;
22

33
public final class Upper {
4-
54
private Upper() {
65
}
76

@@ -16,7 +15,7 @@ public static void main(String[] args) {
1615
}
1716

1817
/**
19-
* Converts all the characters in this {@code String} to upper case.
18+
* Converts all the characters in this {@code String} to upper case
2019
*
2120
* @param s the string to convert
2221
* @return the {@code String}, converted to uppercase.
@@ -29,29 +28,12 @@ public static String toUpperCase(String s) {
2928
if (s.isEmpty()) {
3029
return s;
3130
}
32-
33-
// Check if any lowercase letter exists before creating a new String
34-
boolean hasLower = false;
35-
for (int i = 0; i < s.length(); i++) {
36-
if (Character.isLowerCase(s.charAt(i))) {
37-
hasLower = true;
38-
break;
39-
}
40-
}
41-
42-
// If no lowercase characters, return the same string
43-
if (!hasLower) {
44-
return s;
45-
}
46-
47-
// Convert lowercase letters to uppercase
4831
char[] chars = s.toCharArray();
4932
for (int i = 0; i < chars.length; i++) {
5033
if (Character.isLowerCase(chars[i])) {
5134
chars[i] = Character.toUpperCase(chars[i]);
5235
}
5336
}
54-
5537
return new String(chars);
5638
}
5739
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.thealgorithms.searches;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class TrappingRainWaterTest {
9+
10+
@Test
11+
public void testExample() {
12+
int[] height = {4, 2, 0, 3, 2, 5};
13+
assertEquals(9, TrappingRainWater.trap(height));
14+
}
15+
16+
@Test
17+
public void testEmpty() {
18+
int[] height = {};
19+
assertEquals(0, TrappingRainWater.trap(height));
20+
}
21+
22+
@Test
23+
public void testNoTrapping() {
24+
int[] height = {0, 1, 2, 3, 4};
25+
assertEquals(0, TrappingRainWater.trap(height));
26+
}
27+
28+
@Test
29+
public void testFlat() {
30+
int[] height = {3, 3, 3, 3};
31+
assertEquals(0, TrappingRainWater.trap(height));
32+
}
33+
34+
@Test
35+
public void testNull() {
36+
assertThrows(IllegalArgumentException.class, () -> TrappingRainWater.trap(null));
37+
}
38+
}

0 commit comments

Comments
 (0)