-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrappingRainWater.java
More file actions
74 lines (65 loc) · 2.16 KB
/
Copy pathTrappingRainWater.java
File metadata and controls
74 lines (65 loc) · 2.16 KB
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
import java.util.Arrays;
public class TrappingRainWater {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public int trap(int[] height) {
// find out what heights we need to search
int[] heights = height.clone();
Arrays.sort(heights);
// remove duplicates from height
// a duplicate will be denoted with a -1...Note to self: skip over heights less than curr(starts
// at 0)
int prev = -1;
for (int i = 0; i < heights.length; i++) {
if (heights[i] == prev) {
heights[i] = -1;
} else {
prev = heights[i];
}
}
int v = 0;
int prevHeight = 0;
// loop through each height starting at 0
for (int i = 0; i < heights.length; i++) {
int currHeight = heights[i];
int currLvlTotal = 0;
int currBucket = 0;
int difference = currHeight - prevHeight;
if (currHeight <= 0) {
continue;
} else {
prevHeight = currHeight;
}
// count the volume of water gained at this height
boolean canCollect = false;
for (int j = 0; j < height.length; j++) {
// if can collect water and is less than the current height, we can add one to the volume
if (canCollect == true && height[j] < currHeight) {
// handle the end case
if (j == height.length - 1) {
// if there is no end wall, empty the bucket
currBucket = 0;
} else {
// starts on height something then needs to add the entire bottom
currBucket += difference;
}
} else {
// only occurs when there is some wall
// handle the first wall case
if (canCollect == false && height[j] >= currHeight) {
canCollect = true;
} else {
// second wall was encountered
// add the current bucket which just found an end wall to the level total and set its
// counter to zero
currLvlTotal += currBucket;
currBucket = 0;
}
}
}
v += currLvlTotal;
}
return v;
}
}