码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 11盛水最多的容器

时间:2019-06-12 11:05:01      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:style   双指针   size   while   计算   技术   因此   info   space   

技术图片

 

class Solution {
public:
    int maxArea(vector<int>& height) {
        //双指针法:从最宽的容器开始计算,当更窄的容器盛水量要大于之前容器,那必须比之前容器高,因此可以移动两个指针,直到最窄time O(n),space O(1);
        int low=0;
        int high=height.size()-1;
        int volume=0;
        while(low<high){
            int h=min(height[low],height[high]);
            volume=max(volume,h*(high-low));
            if(height[low]<height[high]){
                low++;
                while(low<high && height[low]<=h) ++low;
            }else{
                high--;
                while(low<high && height[high]<=h) --high;
            }
        }
        return volume;
    }
};

 

leetcode 11盛水最多的容器

标签:style   双指针   size   while   计算   技术   因此   info   space   

原文地址:https://www.cnblogs.com/joelwang/p/11007993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!