11. Container With Most Water

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chineseqsc/article/details/80007093

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 and n 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;
    }
};
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页