forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrappingRainWater.java
More file actions
53 lines (47 loc) · 1.48 KB
/
TrappingRainWater.java
File metadata and controls
53 lines (47 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;
}
}