11. Container With Most Water

Two pointer: start from the beginning and end of array, move forward (backward) on the shorter side.

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

Last updated