-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtrapwater.java
31 lines (27 loc) · 853 Bytes
/
trapwater.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
public class trapingrainwaterr {
public static int trappedrainwater(int h[]){
//left max boundary
int leftmax[]=new int[h.length];
leftmax[0]=h[0];
for(int i=1;i<h.length;i++){
leftmax[i]=Math.max(h[i],leftmax[i-1]);
}
//right max boundary
int rightmax[]=new int[h.length];
rightmax[h.length-1]=h[h.length-1];
for(int i=h.length-2;i>=0;i--){
rightmax[i]=Math.max(h[i],rightmax[i+1]);
}
int trapwater=0;
//loop
for(int i=0;i<h.length;i++){
int waterlevel=Math.min(leftmax[i],rightmax[i]);
trapwater+=waterlevel-h[i];
}
return trapwater;
}
public static void main(String[] args) {
int h[]={4,2,0,6,3,2,5};
System.out.println(trappedrainwater(h));
}
}