-
Notifications
You must be signed in to change notification settings - Fork 22
/
TrappingRainWaterProblem.java
112 lines (99 loc) · 3.36 KB
/
TrappingRainWaterProblem.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.company.amazon;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Problem : 185 Amazon Interview Questions
* <p>
* Input: arr[] = {2, 0, 2}
* Output: 2
* Structure is like below
* | |
* |_|
* We can trap 2 units of water in the middle gap.
* <p>
* <p>
* Input: arr[] = {3, 0, 0, 2, 0, 4}
* Output: 10
* Structure is like below
* |
* | |
* | | |
* |__|_|
* We can trap "3*2 units" of water between 3 an 2,
* "1 unit" on top of bar 2 and "3 units" between 2
* and 4. See below diagram also.
*/
public class TrappingRainWaterProblem {
public static void main(String[] args) {
calculateTrappedRainWater(new Integer[]{2, 0, 2});
calculateTrappedRainWater(new Integer[]{3, 0, 0, 2, 0, 4});
calculateTrappedRainWater(new Integer[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1});
}
public static void calculateTrappedRainWater(Integer[] arr) {
System.out.print("Total RainWaterTrapped in the below structure ");
printArray(arr);
printStructure(arr);
System.out.println("\n is " + calculate(arr));
}
private static int calculate(Integer[] height) {
int LEFT_TOWER_INDEX = 0;
int RIGHT_TOWER_INDEX = height.length - 1;
int MAXIMUM_TOWER_IN_LEFT = 0;
int MAXIMUM_TOWER_IN_RIGHT = 0;
int WATER_TRAPPED = 0;
while (LEFT_TOWER_INDEX < RIGHT_TOWER_INDEX) {
// Find Out which tower is big
if (height[LEFT_TOWER_INDEX] < height[RIGHT_TOWER_INDEX]) {
if (MAXIMUM_TOWER_IN_LEFT < height[LEFT_TOWER_INDEX]) {
MAXIMUM_TOWER_IN_LEFT = height[LEFT_TOWER_INDEX];
} else {
// Trap the water;
WATER_TRAPPED += MAXIMUM_TOWER_IN_LEFT - height[LEFT_TOWER_INDEX];
}
LEFT_TOWER_INDEX++;
} else {
if (MAXIMUM_TOWER_IN_RIGHT < height[RIGHT_TOWER_INDEX]) {
MAXIMUM_TOWER_IN_RIGHT = height[RIGHT_TOWER_INDEX];
} else {
// Trap the water;
WATER_TRAPPED += MAXIMUM_TOWER_IN_RIGHT - height[RIGHT_TOWER_INDEX];
}
RIGHT_TOWER_INDEX--;
}
}
return WATER_TRAPPED;
}
private static void printStructure(Integer[] arr) {
List<Integer> values = Arrays.asList(arr);
int max = Collections.max(values);
boolean[] shouldThisBeIncluded = new boolean[arr.length];
int temp = max;
while (temp > 0) {
for (int j = 0; j < arr.length; j++) {
if (arr[j] == temp || shouldThisBeIncluded[j]) {
System.out.print("|");
if (!shouldThisBeIncluded[j])
shouldThisBeIncluded[j] = true;
} else {
System.out.print(" ");
}
}
System.out.println();
temp--;
}
for (int j = 0; j < arr.length; j++) {
if (arr[j] == 0) {
System.out.print("-");
} else {
System.out.print(" ");
}
}
}
public static void printArray(Integer[] input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + ",");
}
System.out.println();
}
}