-
Notifications
You must be signed in to change notification settings - Fork 0
/
42. Trapping Rain Water.java
62 lines (51 loc) · 1.84 KB
/
42. 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
50
51
52
53
54
55
56
57
58
59
60
61
62
// 42. Trapping Rain Water
// Not optmized solution
class Solution {
public int trap(int[] height) {
if (height == null || height.length == 0) {
return 0;
}
// Solve this by using minimum of local maximas (Find the max of the left side and right side, and use it's minimum, subtract yourself)
int size = height.length;
int maxTotal = 0;
int maxLeft = 0;
int maxRight = 0;
for(int i = 0; i < size - 1; i++) {
maxLeft = height[i];
for(int j = 0; j < i; j++) {
maxLeft = Math.max(maxLeft, height[j]); // max of left hand side
}
maxRight = height[i];
for(int j = i+1; j < size; j++) {
maxRight = Math.max(maxRight, height[j]); // max of right hand side
}
maxTotal = maxTotal + Math.min(maxLeft, maxRight) - height[i]; // min of left & right - current val
}
return maxTotal;
}
}
// O(n) Time solution, optmized
class Solution {
public int trap(int[] height) {
if (height == null || height.length == 0) {
return 0;
}
int result = 0;
int i = 0;
int j = height.length - 1;
int maxLeft = 0;
int maxRight = 0;
while (i < j) {
if (height[i] < height[j]) { // then calculate maxLeft
maxLeft = Math.max(maxLeft, height[i]);
result += maxLeft - height[i];
i++;
} else { // then calculate maxRight;
maxRight = Math.max(maxRight, height[j]);
result += maxRight - height[j];
j--;
}
}
return result;
}
}