LeeCode 11 双指针

题意

传送门 LeeCode 11

题解

双指针的思想是缩小问题规模,对于当前边界高度较小的点,由于以它为边界的最优解已被更新,故可将其去除,缩小区间范围。

class Solution {
public:
    int maxArea(vector<int>& height) {
        int sz = height.size(), s = 0, t = sz - 1, res = 0;
        while(s < t){
            int m = min(height[s], height[t]);
            res = max(res, (t - s) * m);
            if(m == height[s]) ++s;
            else --t;
        }
        return res;
    }
};
发布了110 篇原创文章 · 获赞 1 · 访问量 2036

猜你喜欢

转载自blog.csdn.net/neweryyy/article/details/105596894