-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrappingRainWater.java
More file actions
52 lines (42 loc) · 1.51 KB
/
TrappingRainWater.java
File metadata and controls
52 lines (42 loc) · 1.51 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
49
50
51
52
package stack;
import java.util.Deque;
import java.util.LinkedList;
/**
* Description: https://leetcode.com/problems/trapping-rain-water
* Difficulty: Hard
* Time complexity: O(n)
* Space complexity: O(n)
*/
public class TrappingRainWater {
public int trap(int[] heights) {
Deque<Rectangle> decreasingStack = new LinkedList<>();
int totalAmount = 0;
for (int current = 0; current < heights.length; current++) {
int start = current;
while (!decreasingStack.isEmpty()
&& heights[current] >= decreasingStack.peek().height) {
// 1) 4 2 0 [3]
// 2) pop(0): totalAmount += 1 * (min(2, 3) - 0)
// 3) pop(2): totalAmount += 2 * (min(4, 3) - 2)
// 4) 4 3 []
Rectangle prev = decreasingStack.pop();
if (decreasingStack.isEmpty()) break;
int width = current - prev.start;
int boundaryHeight = Math.min(decreasingStack.peek().height, heights[current]);
int waterHeight = boundaryHeight - prev.height;
totalAmount += width * waterHeight;
start = prev.start;
}
decreasingStack.push(new Rectangle(start, heights[current]));
}
return totalAmount;
}
private static class Rectangle {
int start;
int height;
public Rectangle(int start, int height) {
this.start = start;
this.height = height;
}
}
}