-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution11.java
More file actions
36 lines (30 loc) · 986 Bytes
/
Copy pathSolution11.java
File metadata and controls
36 lines (30 loc) · 986 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
33
34
35
36
package leecode;
/**
* 11. 盛最多水的容器
*/
public class Solution11 {
public static void main(String[] args) {
Solution11 solution11 = new Solution11();
System.out.println(solution11.maxArea(new int[]{4,3,2,1,4}) == 16);
System.out.println(solution11.maxArea(new int[]{1,8,6,2,5,4,8,3,7}) == 49);
System.out.println(solution11.maxArea(new int[]{1,1}) == 1);
}
public int maxArea(int[] height) {
if (height.length == 1)
return 0;
if (height.length == 2)
return Math.min(height[0],height[height.length-1]);
int a = 0, b = height.length-1;
int result = Math.min(height[a],height[b])*b;
while (true){
if (height[a]<=height[b]) {
a++;
} else {
b--;
}
if (a == b)
return result;
result = Math.max(result,Math.min(height[a],height[b])*(b-a));
}
}
}