-
Notifications
You must be signed in to change notification settings - Fork 0
/
trappingRainWater.java
59 lines (49 loc) · 1.06 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
public class trappingRainWater {
static int find_total_water(int[] arr, int n) {
int total_water = 0;
for (int i = 1; i < n - 1; i++) {
int left_max = find_left_max(arr, i, n);
int right_max = find_right_max(arr, i, n);
total_water += min(left_max, right_max) - arr[i];
}
return total_water;
}
static int find_left_max(int[] arr, int i, int n) {
int maximum = 0;
for(int j = 0; j <= i; j++) {
maximum = max(maximum,arr[j]);
}
return maximum;
}
static int find_right_max(int[] arr, int i, int n) {
int maximum = 0;
for(int j = n-1; j >= i; j--) {
maximum = max(maximum,arr[j]);
}
return maximum;
}
static int max(int i, int j) {
int max = 0;
if(i > j) {
max = i;
} else {
max = j;
}
return max;
}
static int min(int i, int j) {
int min = 0;
if(i < j) {
min = i;
} else {
min = j;
}
return min;
}
public static void main(String[] args) {
int[] input2 = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
int n = input2.length;
int result = find_total_water(input2, n);
System.out.println(result);
}
}