-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode11.java
More file actions
30 lines (27 loc) · 836 Bytes
/
LeetCode11.java
File metadata and controls
30 lines (27 loc) · 836 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
public class LeetCode11 {
public static void main(String[] args) {
// 输入:[1,8,6,2,5,4,8,3,7]
// 输出:49
System.out.println(new Solution11().maxArea(new int[] { 1, 8, 6, 2, 5, 4, 8, 3, 7 }));
// 输入:height = [1,1]
// 输出:1
System.out.println(new Solution11().maxArea(new int[] { 1, 1 }));
}
}
class Solution11 {
public int maxArea(int[] height) {
int max = 0;
int left = 0;
int right = height.length - 1;
while (left < right) {
int current = (right - left) * Math.min(height[left], height[right]);
max = Math.max(max, current);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return max;
}
}