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 Solution {
public int maxArea(int[] height) {
int left = 0;
int right = height.length-1;
int max = 0,temp = 0;
while(left<right) {
if(height[left] > height[right]){
temp = (right-left) * height[right];
right--;
}else{
temp = (right-left) * height[left];
left++;
}
max = max > temp ? max :temp;
}
return max;
}
}
//从两端开始遍历数组,每次计算结果,那边数值高,则缩进,以此寻找最小的
//从两端开始遍历数组,每次计算结果,那边数值高,则缩进,以此寻找最小的