-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
33 lines (30 loc) · 1.01 KB
/
Copy pathMain.java
File metadata and controls
33 lines (30 loc) · 1.01 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
// LeetCode Problem: https://leetcode.com/problems/trapping-rain-water/
class Solution {
public int trap(int[] height) {
int left = 0, right = height.length - 1, ans = 0, lMax = Integer.MIN_VALUE, rMax = Integer.MIN_VALUE;
while(left < right){
if(height[left] <= height[right]){
if(lMax < height[left])
lMax = height[left];
else
ans += lMax - height[left];
left++;
} else {
if(rMax < height[right])
rMax = height[right];
else
ans += rMax - height[right];
right--;
}
}
return ans;
}
}
public class Main {
public static void main(String[] args) {
Solution solution = new Solution();
int[] height = {0,1,0,2,1,0,1,3,2,1,2,1};
int trappedWater = solution.trap(height);
System.out.println("Trapped rain water: " + trappedWater);
}
}