-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrapping_Rain_Water.java
More file actions
33 lines (31 loc) · 1.02 KB
/
Copy pathTrapping_Rain_Water.java
File metadata and controls
33 lines (31 loc) · 1.02 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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=scn.nextInt();
}
int[] maxInLeft=new int[n];///max in left
int[] maxInRight=new int[n];////max in roght
maxInLeft[0]=arr[0];
for(int i=1;i<arr.length;i++){
maxInLeft[i]=Math.max(maxInLeft[i-1],arr[i]);
}
maxInRight[n-1]=arr[n-1];
for(int i=n-2;i>=0;i--){
maxInRight[i]=Math.max(arr[i],maxInRight[i+1]);
}
int sum=0;
for(int i=0;i<n;i++){
sum+=Math.min(maxInLeft[i],maxInRight[i])-arr[i];//////total water above each building
}
System.out.println(sum);
}
}