-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrapping_Rain_Water.java
More file actions
32 lines (31 loc) · 947 Bytes
/
Trapping_Rain_Water.java
File metadata and controls
32 lines (31 loc) · 947 Bytes
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
//Link : https://leetcode.com/problems/trapping-rain-water/
public class Trapping_Rain_Water {
//Driver Code
private static int trap(int[] height) {
int[] left = new int[height.length];
int[] right = new int[height.length];
int lmax = 0;
for (int i = 0; i < height.length; i++) {
if (height[i] >= lmax) {
left[i] = height[i];
lmax = height[i];
} else {
left[i] = lmax;
}
}
lmax = 0;
for (int i = height.length - 1; i >= 0; i--) {
if (height[i] > lmax) {
right[i] = height[i];
lmax = height[i];
} else {
right[i] = lmax;
}
}
int count = 0;
for (int i = 0; i < height.length; i++) {
count = count + Math.min(left[i], right[i]) - height[i];
}
return count;
}
}