-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrappingRainWater.java
29 lines (27 loc) · 1.01 KB
/
TrappingRainWater.java
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
public class TrappingRainWater {
public int trap(int[] height) {
int[] maxLeft = new int[height.length];
int[] maxRight = new int[height.length];
int prevLeftMax = 0;
int prevRightMax = 0;
for(int i=0, j=height.length-1; i< height.length && j>=0; i++, j--){
maxLeft[i]= prevLeftMax;
maxRight[j] = prevRightMax;
if(height[i]>prevLeftMax)
prevLeftMax = height[i];
if(height[j] > prevRightMax)
prevRightMax = height[j];
}
int volume =0;
for(int i=0; i<height.length; i++){
int minHeight= Math.min(maxLeft[i], maxRight[i]);
volume+= minHeight-height[i] > 0 ? minHeight-height[i] : 0;
}
return volume;
}
public static void main(String[] args) {
int[] height = {0,1,0,2,1,0,1,3,2,1,2,1};
TrappingRainWater trappingRainWater = new TrappingRainWater();
System.out.println(trappingRainWater.trap(height));
}
}