-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
executable file
·32 lines (29 loc) · 889 Bytes
/
Solution.java
File metadata and controls
executable file
·32 lines (29 loc) · 889 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
32
package $042;
/**
* @author Junlan Shuai[shuaijunlan@gmail.com].
* @date Created on 13:36 2018/5/13.
*/
public class Solution {
public int trap(int[] height){
if (height == null || height.length < 3){
return 0;
}
int leftHeight = height[0];
int rightHeight = height[height.length-1];
int left = 1;
int right = height.length - 2;
int result = 0;
while (left <= right){
if (leftHeight <= rightHeight){
result += Math.max(leftHeight - height[left], 0);
leftHeight = Math.max(leftHeight, height[left]);
left++;
}else {
result += Math.max(rightHeight - height[right], 0);
rightHeight = Math.max(rightHeight, height[right]);
right--;
}
}
return result;
}
}