Clicky

Leetcode 11. Container With Most Water

January 3, 2020

11. Container With Most Water

Result

Runtime: 1411 ms, faster than 5.00% of Java online submissions for Container With Most Water. Memory Usage: 62.6 MB, less than 33.47% of Java online submissions for Container With Most Water.

class Solution {
    int max = 0;
    public int maxArea(int[] height) {
        for(int i=0; i<height.length; i++) {
            if(height[i] * (height.length-i) > max) {
                maxAreaAux(height, i, i);
            }
        }
        return max;
    }
    
    public void maxAreaAux(int[] height, int start, int end) {
        if(end >= height.length)
            return;
        if(start != end) {
            int thisTotal = (end-start) * (Math.min(height[end], height[start]));
            if(thisTotal > max)
                max = thisTotal;
        }
        maxAreaAux(height, start, end+1);
    }
}