-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrap.java
32 lines (30 loc) · 1019 Bytes
/
trap.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
30
31
32
public class trap {
public static int Rainwater(int height[]){
int n=height.length;
//calculate left max boundary -array
int leftmax[]= new int[n];
leftmax[0]=height[0];
for(int i=1; i<n; i++){
leftmax[i]=Math.max(height[i],leftmax[i-1]);
}
//calculate right max boundary -array
int rightmax[]= new int[n];
rightmax[n-1]=height[n-1];
for(int i=n-2;i>=0;i--){
rightmax[i]=Math.max(height[i],leftmax[i+1]);
}
int trappedwat=0;
//loop
for(int i=0;i<n;i++){
//water = min(left max bound,right max bound)
int watlevel= Math.min(leftmax[i],rightmax[i]);
//trapped water = water - height[i]
trappedwat += watlevel-height[i];
}
return trappedwat;
}
public static void main(String args[]){
int height[]={4,2,0,6,3,2,5};
System.out.println(Rainwater(height));
}
}