https://leetcode.com/problems/container-with-most-water/

class Solution {
public:
    int maxArea(vector<int>& height) {
        /*
        逼近法
        两端进行逼近
        每次舍弃短边,因为不存在短边为边的另一个矩形比当前矩形要大【宽变小,高最多为短边】
        */
        #include<algorithm>
        using namespace std;
        int ans=0;
        int l=0,r=height.size()-1;
        while(l<r){
            ans=max(ans,min(height[l],height[r])*(r-l));
            if(height[l]<height[r]) ++l;
            else --r;
        }
        return ans;
    }
};
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。