-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrapRain1.java
More file actions
41 lines (38 loc) · 1.02 KB
/
TrapRain1.java
File metadata and controls
41 lines (38 loc) · 1.02 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
/**
* @description:
* @Author: JachinDo
* @Date: 2019/11/03 20:11
*/
public class TrapRain1 {
public int trap(int[] height) {
int len = height.length;
if (len == 0 || len == 1) {
return 0;
}
int maxH = 0;
int maxHIndex = 0;
int i = 0;
// 找到最大值中的任意一个就行
while (i < len) {
if (height[i] > maxH) {
maxH = height[i];
maxHIndex = i;
}
i++;
}
int total = 0;
// 从左遍历
int maxLeft = height[0];
for (int l = 0; l < maxHIndex; l++) {
maxLeft = Math.max(maxLeft, height[l]);
total = total + Math.min(maxLeft, maxH) - height[l];
}
// 从右
int maxRight = height[len - 1];
for (int r = len - 1; r > maxHIndex; r--) {
maxRight = Math.max(maxRight, height[r]);
total = total + Math.min(maxRight, maxH) - height[r];
}
return total;
}
}