11. Container With Most Water

双指针

class Solution {
public:
    int maxArea(vector<int>& height) {
        //从两边往中间枚举, 保留最大值,优先舍弃最短边
        int ans = 0;
        int l = 0, r = height.size() - 1;
        while(l < r){
            int h = min(height[l], height[r]); //短板原理
            ans = max(ans, h*(r - l)); //保留最大值
            if(height[l] < height[r]) l++;
            else r--;
        }
        return ans;
    }
};
上一篇:leetcode 11. Container With Most Water


下一篇:508. Most Frequent Subtree Sum