-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRainWater.java
More file actions
30 lines (30 loc) · 1.13 KB
/
RainWater.java
File metadata and controls
30 lines (30 loc) · 1.13 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
public class RainWater {
public static int TrappedRainWater(int height[]) { //Time Complixity Of This Code is = O(n)
int n = height.length;
//Calculate LeftMax Boundry - 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 RightMax Boundry -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], RightMax[i+1]);
}
int TrappedWater = 0;
//loop
for(int i=0;i<n;i++){
//WaterLevel = min(LeftMax Boundry , RightMax Boundry)
int waterleverl = Math.min(LeftMax[i], RightMax[i]);
//Trapped Water = WaterLevel - height[i]
TrappedWater += waterleverl - height[i];
}
return TrappedWater;
}
public static void main(String[] args) {
int height[] ={4,2,0,6,3,2,5};
System.out.println("Total TrappedRainWater is : " + TrappedRainWater(height));
}
}