Repository navigation
Expand file tree
/
Copy pathcwmw.java
More file actions
56 lines (54 loc) · 1.57 KB
/
Copy pathcwmw.java
File metadata and controls
56 lines (54 loc) · 1.57 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ContaineWithMostWater011;
/**
* Created by Administrator on 2017/8/16.
*/
public class cwmw {
//方法1,贪心算法
/*public int maxArea(int[] height){
if(height==null||height.length<2)
return 0;
int result=0;
int left=0;
int right=height.length-1;
while(left<right){
result=Math.max(result,Math.min(height[left],height[right])*(right-left));
if (height[left]<height[right]){
int k=left;
while(k<right&&height[k]<=height[left]){
k++;
}
left=k;
}
else{
int k=right;
while(k>left&&height[k]<=height[right]){
k--;
}
right=k;
}
}
return result;
}*/
//方法2,brute force进行夹逼优化,优化没有方法1好,效率比方法1低很多
public int maxArea(int[] height) {
if(height==null || height.length==0)
return 0;
int l = 0;
int r = height.length-1;
int maxArea = 0;
while(l<r)
{
maxArea = Math.max(maxArea, Math.min(height[l],height[r])*(r-l));
if(height[l]<height[r])
l++;
else
r--;
}
return maxArea;
}
public static void main(String[] args){
int[] height={1,2};
int maxArea=new cwmw().maxArea(height);
System.out.print(maxArea);
}
}