Gyh's Braindump

11. Container With Most Water

tags
two pointers
source
Leetcode

Solution

class Solution {
    public int maxArea(int[] height) {
        int l = 0, r = height.length - 1;
        int max = 0;

        while (l <= r) {
            max = Math.max(max, Math.min(height[l], height[r]) * (r - l));
            if (height[l] <= height[r]) {
                l++;
            } else {
                r--;
            }
        }

        return max;
    }
}

Complexity

  • time: O(N)
  • space: O(1)