-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrappingRainWater.java
More file actions
48 lines (46 loc) · 1.46 KB
/
TrappingRainWater.java
File metadata and controls
48 lines (46 loc) · 1.46 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
39
40
41
42
43
44
45
46
47
48
import java.util.Stack;
public class TrappingRainWater {
public int trap(int[] height) {
int left = 0;
int right = height.length - 1;
int res = 0;
int leftMax = 0;
int rightMax = 0;
while (left < right) {
if (height[left] < height[right]) {
leftMax = Math.max(height[left], leftMax);
res += leftMax - height[left];
left++;
} else {
rightMax = Math.max(height[right], rightMax);
res += rightMax - height[right];
right--;
}
}
return res;
}
public int trap1(int[] A) {
if (A==null) return 0;
Stack<Integer> s = new Stack<Integer>();
int i = 0, maxWater = 0, maxBotWater = 0;
while (i < A.length){
if (s.isEmpty() || A[i]<=A[s.peek()]){
s.push(i++);
}
else {
int bot = s.pop();
maxBotWater = s.isEmpty()? // empty means no il
0:(Math.min(A[s.peek()],A[i])-A[bot])*(i-s.peek()-1);
maxWater += maxBotWater;
}
}
return maxWater;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int s[]= {0,1,0,2,1,0,1,3,2,1,2,1};
int num1[]= {2,1,0,3,0,0,4};
int n=new TrappingRainWater().trap1(num1);
System.out.println(n);
}
}