-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrappingRainwater.java
More file actions
38 lines (38 loc) · 1.19 KB
/
TrappingRainwater.java
File metadata and controls
38 lines (38 loc) · 1.19 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
import java.util.*;
public class TrappingRainwater {
public static void main(String[] args) {
Scanner ab=new Scanner(System.in);
System.out.println("Enter the number of buildings :");
int n=ab.nextInt();
int[] height= new int[n];int i=0;
for(i=0;i<n;i++){
System.out.println("Enter the different heights of the buildings : \n");
height[i]=ab.nextInt();
}
TrappingRainwater ob=new TrappingRainwater();
int ans=ob.trapped(height);
System.out.println("Total water trapped is6 : \n"+ans);
}
int trapped(int[] height){
int n=height.length;
int[] left= new int[n];
int[] right=new int[n];
left[0]=height[0];
right[n-1]=height[n-1];
int ans=0,i=0;
for (i = 1; i < n; i++) {
left[i]=Math.max(height[i], left[i-1]);
}
for (i = n-2; i>=0; i--) {
right[i]=Math.max(height[i], right[i+1]);
}
int wblock=0;
for (i = 0; i < n; i++) {
wblock=Math.min(left[i],right[i]);
if(height[i]<wblock){
ans += wblock-height[i];
}
}
return ans;
}
}