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.
假设两条线是(i, ai)和(j, aj);那么最大的值应该是(j-i)*min(ai,aj);
我们可以从分别从两端开始遍历,高小的那一端向前进一步,每次计算当前的面积(索性就当是面积吧)并更新最大值
public class Solution {
public int maxArea(int[] height)
{
int s=0;
int n=height.length;
int width=0,i=n-1,j=0;
int temp=0;
while(j<=i)
{
width=i-j;
if(height[j]<height[i])
{
s=height[j]*width>s?height[j]*width:s;
j++;
}
else
{
s=height[i]*width>s?height[i]*width:s;
i--;
}
}
return s;
}
}