leetcode-盛最多水的容器

 

class Solution {
public:
    int maxArea(vector<int>& height) {
        int i = 0;
        int j = height.size()-1;
        int res = -1;
        while(i<j){
            res = max(res,min(height[j],height[i])*(j-i));
            // 每次移动短板,如果移动长板,res的值只会小于或等于上一个res
            if(height[i]<=height[j])
                i++;
            else
                j--;

        }
        return res;
    }
};

 

posted @ 2021-07-14 19:01  三一一一317  阅读(24)  评论(0)    收藏  举报