Skip to content

Instantly share code, notes, and snippets.

@Ephraimiyanda
Created January 15, 2026 16:03
Show Gist options
  • Select an option

  • Save Ephraimiyanda/d6c8a384ecaac723e1fb73fe6bc161d9 to your computer and use it in GitHub Desktop.

Select an option

Save Ephraimiyanda/d6c8a384ecaac723e1fb73fe6bc161d9 to your computer and use it in GitHub Desktop.
Container With Most Water

Question

Approach

To get the area between poles i created two pointers for the pair of poles that moves inward while calculating the areas between the poles also while limiting the height to be used for calculating the area to the shorter pole.

Complexity

  • Time complexity: O(N)

  • Space complexity: O(1)

Code

function maxArea(height: number[]): number {
    let areas = []
    let i = 0;
    let j = height.length - 1

    while (j > i) {
        let width = j - i
        if (height[i] < height[j]) {
            areas.push(height[i] * width)
            i++
        } else {
            areas.push(height[j] * width)
            j--
        }
    }

    return Math.max(...areas)
};
scrnli_ECy4a27UnMLZZ0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment