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.
左右指针往中间靠拢。
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
var i=0;
var j=height.length-1;
var left_flag;
var right_flag;
var m_area = 0;
var left=height[i];
var right = height[j];
while(i!==j){
if(left<right){
var new_area = (j-i)*left;
if(new_area>m_area){
m_area = new_area;
left_flag = i;
}
i++;
left = height[i];
}else{
new_area = (j-i)*right;
if(new_area>m_area){
right_flag = j;
m_area = new_area;
}
j--;
right = height[j];
}
}
return m_area;
};