-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode42.java
More file actions
74 lines (68 loc) · 2 KB
/
LeetCode42.java
File metadata and controls
74 lines (68 loc) · 2 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
71
72
73
74
public class LeetCode42 {
public static void main(String[] args) {
int[] height;
// 输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
// 输出:6
height = new int[] { 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1 };
System.out.println(new Solution42_2().trap(height));
// 输入:height = [4,2,0,3,2,5]
// 输出:9
height = new int[] { 4, 2, 0, 3, 2, 5 };
System.out.println(new Solution42_2().trap(height));
}
}
/**
* 寻找可以盛水的区间
*/
class Solution42_1 {
public int trap(int[] height) {
if (height.length <= 1) {
return 0;
}
int start = 0;
int end = 1;
int sum = 0;
while (start < height.length - 1) {
if (height[end] >= height[start]) {
for (int i = start + 1; i < end; i++) {
sum += height[start] - height[i];
}
start = end;
} else {
// 如果搜索到最后,还没找到更高的右边界,就利用这个最高点,搜索比它矮一点的右边界
if (end == height.length - 1) {
height[start] -= 1;
end = start;
}
}
end++;
}
return sum;
}
}
/**
* DP查找节点两边的最大值
*/
class Solution42_2 {
public int trap(int[] height) {
int n = height.length;
if (n == 0) {
return 0;
}
int[] leftMax = new int[n];
leftMax[0] = height[0];
for (int i = 1; i < n; ++i) {
leftMax[i] = Math.max(leftMax[i - 1], height[i]);
}
int[] rightMax = new int[n];
rightMax[n - 1] = height[n - 1];
for (int i = n - 2; i >= 0; --i) {
rightMax[i] = Math.max(rightMax[i + 1], height[i]);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += Math.min(leftMax[i], rightMax[i]) - height[i];
}
return ans;
}
}