Given n non-negative integers , where each represents a point at coordinate . n vertical lines are drawn such that the two endpoints of line is at and . 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 and is at least 2.
以头尾两根为基准(底最长),其他组合要大于此容积必须大于它的高(取决于短板)
class Solution {
public:
int maxArea(vector<int>& height) {
int n = height.size();
int i = 0, j = n - 1;
int maxC = INT_MIN;
while(i < j)
{
int minH = min(height[i] , height[j]);
maxC = max(maxC , minH * (j - i));
while(height[i] <= minH && i < j)
i ++;
while(height[j] <= minH && i < j)
j --;
}
return maxC;
}
};