Given n non-negative integers a1 , a2 , ..., an , where each represents a point at coordinate (i, ai ). n vertical lines are drawn such that the two endpoints of line i is at (i, ai ) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.
就是取最短板问题
如果左边的比右边短,重新取左边,直到找到更长的,令一端亦然。直到两边相交。结束。取出最大的面积。
public class MaxArea {
public int maxArea(int[] height) {
int high = height.length-1;
int low = 0;
int area = 0;
int max = 0;
while(high>low) {
if(height[high]>height[low]) {
area = (high-low)*height[low];
low++;
}else {
area = (high-low)*height[high];
high--;
}
if(max<area)max = area;
}
return max;
}
}