-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathTrappingRainWater.java
60 lines (50 loc) · 1.38 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution {
// TC: O(n)
// SC : O(n)
public int trap(int[] height) {
if(height.length <=0){
return 0;
}
int n = height.length;
int[] left = new int[n];
int max =height[0];
for(int i=0;i<n;i++){
max = Math.max(max, height[i]);
left[i] = max;
}
int[] right = new int[n];
max =height[n-1];
for(int i=n-1;i>=0;i--){
max = Math.max(max, height[i]);
right[i] = max;
}
int totalWater = 0;
for(int i=0;i<n;i++){
totalWater += Math.min(left[i] , right[i]) - height[i];
}
return totalWater;
}
}
// TC : O(n)
// SC : O(n)
class Solution {
public int trap(int[] height) {
if (height.length <=1) return 0;
Stack<Integer> stack = new Stack<>();
int water = 0, i = 0;
while (i < height.length) {
if (stack.isEmpty() || height[i] <= height[stack.peek()]) {
stack.push(i++);
} else {
int currIndex = stack.pop();
if (!stack.isEmpty()) {
// find the smaller height between the two sides
int minHeight = Math.min(height[stack.peek()], height[i]);
// calculate the area
water += (minHeight - height[currIndex]) * (i - stack.peek() - 1);
}
}
}
return water;
}
}