美文网首页
container-with-most-water

container-with-most-water

作者: DaiMorph | 来源:发表于2019-07-21 22:58 被阅读0次

这题最关键的是两点,一是两边往中间找,二是每次放弃最短的版。

这么做的原因在于:从起点和终点开始找,宽度最大,这时每移动一次其中一个点,必然宽度变小。

如此一来,想求最大,只有高度增长才有可能做到,即放弃高度较小的点。

class Solution {
public:
    int maxArea(vector<int> &height) {
        int res=0,l=0,r=height.size()-1;
        while(l<r)
        {
            res=max(res,min(height[l],height[r])*(r-l));
            if(height[l]<height[r])l++;
            else r--;
        }
        return res;
    }
};

相关文章

  • 11.盛最多水的容器

    https://leetcode-cn.com/problems/container-with-most-water/

  • container-with-most-water

    这题最关键的是两点,一是两边往中间找,二是每次放弃最短的版。 这么做的原因在于:从起点和终点开始找,宽度最大,这时...

网友评论

      本文标题:container-with-most-water

      本文链接:https://www.haomeiwen.com/subject/sulblctx.html