-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0042.java
More file actions
70 lines (62 loc) · 2.52 KB
/
Copy pathLeetCode0042.java
File metadata and controls
70 lines (62 loc) · 2.52 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Trapping Rain Water
* Example:
* Input: [0,1,0,2,1,0,1,3,2,1,2,1]
* Output: 6
* */
import java.util.Stack;
public class LeetCode0042 {
public static void main(String args[]) {
int[] height = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
System.out.println(trap(height));
}
//Two pointers
public static int trap(int[] height) {
if (height.length == 0)
return 0;
int ans = 0;
int len = height.length;
int[] Lmax = new int[len];
int[] Rmax = new int[len];
Lmax[0] = height[0];
Rmax[len - 1] = height[len - 1];
//找到左边最大高度
for (int i = 1; i < len; i++)
Lmax[i] = Math.max(height[i], Lmax[i - 1]);
//找到右边最大高度
for (int i = len - 2; i >= 0; i--)
Rmax[i] = Math.max(height[i], Rmax[i + 1]);
//积水=当前位置 最大高度-height
for (int i = 0; i < len - 1; i++) {
ans += Math.min(Lmax[i], Rmax[i]) - height[i];
}
return ans;
}
//Monotonic stack 单调栈
//利用栈单调性,如果栈中的元素是[3 1 0],下一个元素是2,那么我们就可以知道前面很可能有一个坑,
//将 0 pop出来之后,那么计算该处的水的量即:
//找到两边极小值(在1 0 2中是 1),那么可容水量即为 ( 2 - 1 )* (1 - 0 ) = 1,后面依次计算即可。
public static int trapStack(int[] height) {
if (height.length == 0)
return 0;
Stack<Integer> stack = new Stack<>();
int i, sum = 0;
int len = height.length;
for (i = 0; i < len; i++) {
int width = 0; //宽度初始化
while (!stack.empty() && height[stack.peek()] < height[i]) //如果栈非空并且满足栈顶元素小于即将进入的元素
{
int temp = stack.peek();
stack.pop();
if (stack.empty())
break;
if (height[temp] == height[stack.peek()])
continue;
width = i - stack.peek() - 1; //宽度计算
int minHeight = Math.min(height[i], height[stack.peek()]); //最小高度
sum += width * (minHeight - height[temp]); //容量累计
}
stack.push(i);
}
return sum;
}
}