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 and n is at least 2.
class Solution {
public int maxArea(int[] height) {
int length = height.length;
int l = 0;
int r = length-1;
int area = 0;
while(l<r){
//从两边开始,取最大底边,此时是一种极限情况。
//然后比较左边和右边的高,矮边朝中间靠拢
int temp = Math.max(area,Math.min(height[l],height[r])*(r-l));
if(temp > area){
area = temp;
}
if(height[l]<height[r]){
l++;
}
else
r--;
}
return area;
}
}