forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrappingRainWater.java
More file actions
35 lines (30 loc) · 973 Bytes
/
TrappingRainWater.java
File metadata and controls
35 lines (30 loc) · 973 Bytes
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
/**
* 核心思路就是对于每根柱子,找到其左边最高的柱子和右边最高的柱子,构成一个桶,形成一个水平面,然后对该柱子形成的高度差就是能装的水
*/
public class TrappingRainWater {
// 耗时24ms
public int trap(int[] height) {
int len = height.length;
if (len == 0) {
return 0;
}
int[] left = new int[len];
int[] right = new int[len];
left[0] = 0;
right[len - 1] = 0;
for (int i = 1; i < len; i++) {
left[i] = Math.max(left[i - 1], height[i - 1]);
}
for (int i = len - 2; i>= 0; i--) {
right[i] = Math.max(right[i + 1], height[i + 1]);
}
int sum = 0;
for (int i = 0; i < len; i++) {
int high = Math.min(left[i], right[i]);
if (high > height[i]) {
sum += high - height[i];
}
}
return sum;
}
}