Leetcode 11. Container With Most Water

Two Pointer Algorithm & Proof

class Solution {
    public int maxArea(int[] height) {
        int left=0;
        int right=height.length-1;
        int sum=0;
        while(left<right){
            if(Math.min(height[right],height[left])*(right-left)>sum){
                sum=Math.min(height[right],height[left])*(right-left);
            }
            if(height[right]<height[left]){
                right=right-1;
            }
            else{
                left=left+1;
            }
        }
        return sum;
    }
}

Proof:
https://segmentfault.com/a/1190000008824222
https://yq.aliyun.com/news/242671

你可能感兴趣的:(leetcode,two,pointer,like,Leetcode)