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.
思路:暴力法是枚举所有可能的组合,可以用贪心法,每次替换最短line。
#include <algorithm>
#include <limits.h>
class Solution {
public:
int maxArea(vector<int>& height) {
int start = 0;
int end = height.size() - 1;
int maxV = INT_MIN;
while(start < end)
{
int contain = min(height[end], height[start])*(end - start);//area
maxV = max(maxV, contain);
if(height[start] <= height[end])
{
start++;
}
else{
end--;
}
}
return maxV;
}
};