zoukankan      html  css  js  c++  java
  • [leetcode]Container With Most Water

    #include <iostream>
    #include <vector>
    using namespace std;
    
    class Solution {
    public:
        int maxArea(vector<int> &height) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if (height.size() < 2)
                return 0;
    
            int l = 0;
            int r = height.size() - 1;
            int lCurrMax = height[l];
            int rCurrMax = height[r];
            int areaMax = (r-l)*min(lCurrMax, rCurrMax);
    
            while (l+1<r){
                if (lCurrMax < rCurrMax){
                    if (height[l+1] <= height[l]){
                        l++;
                    }
                    else{
                        l++;
                        lCurrMax = height[l];
                        areaMax = max(areaMax, (r-l)*min(height[l], height[r]));
                    }
                }
                else{
                    if (height[r-1] <= height[r]){
                        r--;
                    }
                    else{
                        r--;
                        rCurrMax = height[r];
                        areaMax = max(areaMax, (r-l)*min(height[l], height[r]));
                    }
                }
            }
    
            return areaMax;
        }
    };
    
    int main()
    {
        vector<int> height;
        height.push_back(1);
        height.push_back(2);
        height.push_back(4);
        height.push_back(3);
        Solution s;
        int areaMax = s.maxArea(height);
        return 0;
    }

    EOF

  • 相关阅读:
    php 函数汇总
    php 图片base64编码生成dataurl和保存为图片
    bootstrap
    PHPWord
    js json排序
    UE用法
    判断移动端是苹果还是安卓,以及安卓版本
    shell终极操作
    LINUX yum用法
    jquery对checkbox的操作汇总
  • 原文地址:https://www.cnblogs.com/lihaozy/p/2839864.html
Copyright © 2011-2022 走看看