forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0042-trapping-rain-water.java
49 lines (40 loc) · 1.24 KB
/
0042-trapping-rain-water.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
class Solution {
public int trap(int[] heights) {
int left[] = new int[heights.length], right[] = new int[heights.length], max =
heights[0], c = 0;
for (int i = 0; i < heights.length; i++) {
left[i] = Math.max(heights[i], max);
max = left[i];
}
max = heights[heights.length - 1];
for (int i = heights.length - 1; i >= 0; i--) {
right[i] = Math.max(heights[i], max);
max = right[i];
}
for (int i = 0; i < heights.length; i++) {
c = c + Math.min(left[i], right[i]) - heights[i];
}
return c;
}
}
//O(1) space
class Solution {
public int trap(int[] heights) {
if (heights.length == 0) return 0;
int l = 0, r = heights.length - 1;
int leftMax = heights[l], rightMax = heights[r];
int res = 0;
while (l < r) {
if (leftMax < rightMax) {
l++;
leftMax = Math.max(leftMax, heights[l]);
res += leftMax - heights[l];
} else {
r--;
rightMax = Math.max(rightMax, heights[r]);
res += rightMax - heights[r];
}
}
return res;
}
}