forked from yuduozhou/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrappingRainWater.java
86 lines (80 loc) · 1.95 KB
/
TrappingRainWater.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
public class Solution {
public int trap(int[] A) {
// Start typing your Java solution below
// DO NOT write main() function
int n = A.length;
if (n < 3) return 0;
int[] lmax = new int[n];
int[] rmax = new int[n];
lmax[0] = A[0];
for (int i = 1; i < n; i++){
lmax[i] = (lmax[i - 1] > A[i]) ? lmax[i - 1] : A[i];
}
rmax[n - 1] = A[n - 1];
for (int i = n - 2; i >= 0; i--){
rmax[i] = (rmax[i + 1] > A[i]) ? rmax[i + 1] : A[i];
}
int total = 0;
for (int i = 1; i < n - 1; i++){
int low = (lmax[i - 1] < rmax[i + 1]) ? lmax[i - 1] : rmax[i + 1];
total += (low > A[i]) ? (low - A[i]) : 0;
}
return total;
}
}
/* Recursion */
public class Solution {
public static int trap(int[] A) {
// Start typing your Java solution below
// DO NOT write main() function
int n = A.length;
if(n <3){
return 0;
}
int capacity = 0;
int max = 0;
for (int i = 0; i < n; i++){
if (A[i] > A[max]){
max = i;
}
}
capacity = calculateVolume(0, max, A) + calculateVolume(max, n - 1, A);
return capacity;
}
public static int calculateVolume(int l, int r, int[] A){
// Suppose i <= j
if ((l - r) * (r - l) == -1 ||l==r ){
return 0;
}
int capacity = 0;
int max = -1;
if (A[l] < A[r]){
max = l;
for (int i = l; i < r; i++){
if (A[i] > A[max]){
max = i;
}
}
// Calcualate volume between max and A[r]
for (int i = max + 1; i < r; i++){
capacity += A[max] - A[i];
}
capacity += calculateVolume(l, max, A);
return capacity;
}
else{ // A[l] >= A[r], right half
max = r;
for (int i = r; i > l; i--){
if (A[i] > A[max]){
max = i;
}
}
// Calcualate volume between max and A[r]
for (int i = max - 1; i > l; i--){
capacity += A[max] - A[i];
}
capacity += calculateVolume(max, r, A);
return capacity;
}
}
}