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.
好水的一道题,结果我想多了,还想用区间dp23333333,思路就是一个简单贪心,其实也有想过贪心,不过方向不对,哎233333
class Solution {
public:
int maxArea(vector<int>& height) {
int len = height.size();
int maxx = 0;
int l = 0;
int r = len - 1;
while(l < r)
{
maxx = max(maxx, min(height[l], height[r]) * (r - l));
if(height[l] < height[r])
l++;
else
r--;
}
return maxx;
}
};