[每日一题2020.06.22]leetcode #11 双指针

1592826766978

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

解 : 移动高的那一端不会得到更优的结果, 所以我们每次只需要移动矮的那一端即可

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

猜你喜欢

转载自www.cnblogs.com/roccoshi/p/13178801.html