美文网首页
11. 盛最多水的容器

11. 盛最多水的容器

作者: vancymoon | 来源:发表于2021-03-18 01:37 被阅读0次

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

  1. 从两端开始,向内移动
  2. 每次移动小的一端,因为它在接下来的遍历中,不可能充当面积最大的一条边
class Solution {
public:
    int maxArea(vector<int>& height) {
        int ret(0);
        for (int left = 0, right = height.size() - 1; left != right;) {
            int min_height = height[left] <= height[right] ? height[left++] : height[right--];
            ret = max(ret, (right - left + 1) * min_height);
        }
        return ret;
    }
};

相关文章

网友评论

      本文标题:11. 盛最多水的容器

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