-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path42.java
50 lines (45 loc) · 1.01 KB
/
42.java
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
class Solution42 {
public static void main(String args[]) {
Solution42 s = new Solution42();
int[] height = new int[] { 4, 3, 3, 9, 3, 0, 9, 2, 8, 3 };
s.trap(height);
}
public int trap(int[] height) {
int res = 0;
boolean overlap = false;
int highest_trap = 0;
int max = 0;
int potential = 0;
for (int i = 0; i < height.length; i++) {
int cur = height[i];
if (cur >= max) {
res += potential;
potential = 0;
highest_trap = max;
max = cur;
} else {
potential += max - cur;
}
}
if (res > 0 && max == highest_trap) {
overlap = true;
}
max = 0;
potential = 0;
for (int i = height.length - 1; i >= 0; i--) {
int cur = height[i];
if (cur >= highest_trap && overlap) {
res += potential;
break;
}
if (cur >= max) {
res += potential;
potential = 0;
max = cur;
} else {
potential += max - cur;
}
}
return res;
}
}