leetcode水题 计算容器最大装水量
思路很简单,就是首先找到最右边和最左边构成一个容器,计算它的容积,然后如果最左边的边比最右边的边短,高度取决于最左边的边,那么最右边的边改变的话只会将宽越变越小,所以只有可能变左边的边,使得高度变高,宽度变窄。反之一样。

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

相关文章:

  • 2021-11-03
  • 2021-06-06
  • 2021-10-20
  • 2021-07-03
  • 2022-01-01
  • 2022-01-16
  • 2021-10-19
  • 2021-06-05
猜你喜欢
  • 2021-10-11
  • 2022-12-23
  • 2021-04-13
  • 2021-12-01
  • 2021-09-14
  • 2021-05-31
  • 2021-10-28
相关资源
相似解决方案