-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrappingWater.java
58 lines (42 loc) · 1.51 KB
/
TrappingWater.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
package com.company.LeetCode;
// https://leetcode.com/problems/trapping-rain-water/
public class TrappingWater {
public static void main(String[] args) {
int[] height = {0,1,0,2,1,0,1,3,2,1,2,1};
System.out.println(trap(height));
}
static public int trap(int[] height) {
int[] lmax = new int[height.length];
int[] rmax = new int[height.length];
int res = 0;
lmax[0] = height[0];
rmax[height.length - 1] = height[height.length - 1];
for(int i = 1; i < height.length - 1; i++){
lmax[i] = Math.max(height[i], lmax[i - 1]);
}
for(int i = height.length - 2; i >= 0; i--){
rmax[i] = Math.max(height[i], rmax[i + 1]);
}
for(int i = 1; i < height.length - 1; i++){
res += Math.min(lmax[i], rmax[i]) - height[i];
}
return res;
// int res = 0;
// int left = 0;
// int right = height.length - 1;
// int maxl = 0, maxr = 0;
// while(left < right){
// if(height[left] < height[right]){
// if(height[left] >= maxl) maxl = height[left];
// else res += maxl - height[left];
// left++;
// }
// else {
// if(height[right] >= maxr) maxr = height[right];
// else res += maxr - height[right];
// right--;
// }
// }
// return res;
}
}