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.简单点说就是找两条刻度线的短板,求这条短板与x轴围成的面积(最大值)
public class Solution { //时间复杂度O(logn)
public int maxArea(int[] height) {
int maxArea = 0;
int l = 0;
int h = height.length - 1;
while (l < h){
maxArea = Math.max(Math.min(height[l], height[h]) * (h - l), maxArea);
if (height[l] < height[h]){
l ++;
}
else {
h --;
}
}
return maxArea;
}
}