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) {
if(height.size()<=1)
return 0;
int beg=0;
int end=height.size()-1;
int sum=0;
while(beg<end)
{
int min_va=min(height[beg],height[end]);
int temp=(end-beg)*min_va;
if(temp>sum)
sum=temp;
if(height[beg]>height[end])
--end;
else
++beg;
}
return sum;
}
};