-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.盛最多水的容器.java
36 lines (35 loc) · 891 Bytes
/
11.盛最多水的容器.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
/*
* @lc app=leetcode.cn id=11 lang=java
*
* [11] 盛最多水的容器
*/
// @lc code=start
class Solution {
public int maxArea(int[] height) {
// two-pointer
int i = 0, j = height.length - 1;
int max = 0;
while (i < j) {
max = Math.max(max, (j - i) * Math.min(height[i], height[j]));
if (height[i] < height[j]) {
int k = i + 1;
for (; k < j; k++) {
if (height[k] > height[i]) {
break;
}
}
i = k;
} else {
int k = j - 1;
for (; k > i; k--) {
if (height[k] > height[j]) {
break;
}
}
j = k;
}
}
return max;
}
}
// @lc code=end