Example: Container With Most Water - rFronteddu/general_wiki GitHub Wiki

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.

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

        while (l < r) {
            // keep going until pointers cross

            int currArea = Math.min (height[l], height[r]) * (r-l);
            maxArea = Math.max (maxArea, currArea);

            // move the pointer of the smaller container inward since that is the current cap
            if (height[l] < height[r]) {
                l++;
            } else {
                r--;
            }
        }
        return maxArea;
    }
}