-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathTrappingRainWater.java
45 lines (37 loc) · 1.15 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Scanner;
class Solution {
public static int trap(int[] height) {
int n = height.length;
int res = 0;
int[] lmax = new int [n];
int[] rmax = new int [n];
lmax[0]=height[0];
for(int i = 1;i<n;i++){
lmax[i]=Math.max(height[i],lmax[i-1]);
}
rmax[n-1]=height[n-1];
for(int i=n-2;i>=0;i--){
rmax[i]=Math.max(height[i],rmax[i+1]);
}
for(int i =1;i<n-1;i++){
res = res+(Math.min(lmax[i],rmax[i])-height[i]);
}
return res;
}
}
public class TrappingRainWater {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = sc.nextInt();
int[] height = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
height[i] = sc.nextInt();
}
Solution solution = new Solution();
int trappedWater = solution.trap(height);
System.out.println("The amount of trapped rainwater is: " + trappedWater);
sc.close();
}
}