题解 | #盛水最多的容器#

盛水最多的容器

https://www.nowcoder.com/practice/3d8d6a8e516e4633a2244d2934e5aa47

class Solution {
public:
   //双指针算法
    int maxArea(vector<int>& height) 
    {
        int n=height.size();
        int left=0,right=n-1;
        int res=0;
        while(left<right)
        {
            int v=min(height[left],height[right])*(right-left);
            res=max(v,res);
            if(height[right]>height[left])left++;
            else right--;
        }
        return res;
    }
};

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务