-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrappingRainWater.java
123 lines (103 loc) · 4.1 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
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
113
114
115
116
117
118
119
120
121
122
123
package Algorithms.TwoPointers;
/**
* <pre>
* reed trapMyThoughts() documentation at below
3 ********
| * * ❌ ❌ ❌ ❌
| * *
2 ******** * ******** ********
| * * * * * * *
| * * 💧 💧 💧 * * * 💧 * * ❌
| * * * * * * *
1 ******** * ******** ******** * ******** ********
| ❌ * * 💧 * * * 💧 * * * * * * *
|____ 0 ____ 1 ____ 0 ____ 2 ____ 1 ____ 0 ____ 1 ____ 3 ____ 2 ____ 1 ____ 2 ____ 1
0i 1i 2i 3i 4i 5i 6i 7i 8i 9i 10i 11i 12i
</pre>
* @author Srinvas Vadige, srinivas.vadige@gmail.com
* @since 02 Oct 2024
*/
public class TrappingRainWater {
public static void main(String[] args) {
int[] height = new int[]{0,1,0,2,1,0,1,3,2,1,2,1};
System.out.println(trap(height));
}
// l at 0, r at length - 1
public static int trap(int[] height) {
int left = 0;
int right = height.length - 1;
int leftMax = height[left];
int rightMax = height[right];
int water = 0;
while (left < right) {
if (leftMax < rightMax) { // to avoid right most overflow ❌ in the diagram
left++;
leftMax = Math.max(leftMax, height[left]);
water += leftMax - height[left];
} else {
right--;
rightMax = Math.max(rightMax, height[right]);
water += rightMax - height[right];
}
}
return water;
}
public static int trap2(int[] height) {
int left = 0;
int right = height.length - 1;
int leftMax = 0;
int rightMax = 0;
int total = 0;
while (left < right) {
if (height[left] < height[right]) {
leftMax = Math.max(leftMax, height[left]);
total += Math.min(leftMax, height[left]) - height[left];
left++;
} else {
rightMax = Math.max(rightMax, height[right]);
total += Math.min(rightMax, height[right]) - height[right];
right--;
}
}
return total;
}
/**
* <pre>
water stagnates only if lh>rh upto lh<=rh
need bigger heights
calculate hight diffs in each r loop with current l
scenario is changing after r == height.length-1
If we move both pointers to right side
And now let's the r pointer just passed the biggest of all heights
then we are keeping that biggest height as l
Once we reached lh <= rh it makes us count the dummies even if we don't have lh <= rh height
So, it's better to start with two pointers l == 0 and r == n-1
And loop untill l index < r index by moving both pointers at once and
have lMax height and rMax height.
or l=0 and r=0 with lMax and rMax
</pre>
*/
public static int trapMyThoughts(int[] height) {
int l = 0, r =1;
int units = 0;
int tempUnits = 0; // as we are not sure if we have bigger r in future
//int tempHeight = 0; // my thoughts on this
while(l < height.length){
System.out.println("l:" +l + ", r:" + r);
if(height[l]>height[r]){
tempUnits += height[l]-height[r];
System.out.println("inside tempUnits: " + tempUnits);
}
if(height[l] <= height[r]){
units += tempUnits;
tempUnits=0;
l = r;
System.out.println("--------- inside Units: " + units);
}
if (r < height.length-1)
r++;
else l++;
}
return units;
}
}