leetcode.11. Container With Most Water

转向c++了,底乘高,低最大,面积再想大一点就得

class Solution {
public:
    int maxArea(vector<int>& height)
    {
        int maxcon=0;
        int i=0;
        int  h;
        int j=height.size()-1;
        while(i<j)
        {
            h=min(height[i],height[j]);
           //printf("%d %d %d",h,j,i);
            maxcon=max(maxcon,h*(j-i));
            //printf("\n%d",maxcon);
            while (height[i]<=h) i++;
            while (height[j]<=h) j--;
        }
      return maxcon;
    }
};

保证高不断拓高,低的那一边就需要往上升到比原来高的地方才能提高容器。

猜你喜欢

转载自www.cnblogs.com/yuhaowang/p/10238326.html