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.
贪心算法,不断更新左右下标和最大面积class Solution {
public:
int maxArea(vector<int> &height)
{
int size = height.size();
if(size < 2)
return 0;
int left = 0, right = size-1;
int maxarea = 0;
while(left < right)
{
int smaller = height[left] > height[right] ? height[right] : height[left];
int area = (right - left) * smaller;
maxarea = maxarea > area ? maxarea : area;
if(height[left] <= height[right])
{
left++;
}
else
right--;
}
return maxarea;
}
};