-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLt42.java
More file actions
31 lines (27 loc) · 781 Bytes
/
Lt42.java
File metadata and controls
31 lines (27 loc) · 781 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
package array;
class Lt42 {
// 투포인터
public int trap(int[] height) {
int left = 0, right = height.length -1;
int leftMax = 0, rightMax = 0;
int water = 0;
while (left < right) {
if (height[left] < height[right]) {
if (height[left] >= leftMax) {
leftMax = height[left];
} else {
water += leftMax - height[left];
}
left++;
} else {
if (height[right] >= rightMax) {
rightMax = height[right];
} else {
water += rightMax - height[right];
}
right--;
}
}
return water;
}
}