题目的大概意思如下:
给定一个数组,从数组中选择出对应的(i,ai)和(i, 0)坐标点,像X轴做垂直线形成一个容器,求容器最大的容积。
思路:
我们从数组的左边和右边作为其实点,分别计算出对应的容积,并进行数组元素的比较,如果左边的数组元素大于右边的数组元素,将数组右边的元素进行左移操作。
代码如下:
public static int maxArea(int[] height) {
if(height.length<2)
return 0;
int max =0;
int start=0;
int end =height.length-1;
while(start<end){
int area = Math.min(height[start], height[end])*(end-start);
max=Math.max(max, area);
if (height[start]>height[end]){
end--;
}else{
start++;
}
}
return max;
}