Container With Most Water

来源:互联网 发布:阿里妈妈淘宝客采集器 编辑:程序博客网 时间:2024/05/09 23:30
类似于2Sum的思想,两边设一个指针,然后计算area,如果height[i] <= height[j],那么i++,因为在这里height[i]是瓶颈,j往里移只会减少面积,不会再增加area。
class Solution {public:    int maxArea(vector<int> &height) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        int left=0,right=height.size()-1,maxA=0,area=0;        while(left<right){            area=min(height[left],height[right])*(right-left);            maxA=max(maxA,area);            if(height[left]>=height[right]){                --right;            }else{                ++left;            }        }        return maxA;    }};

原创粉丝点击