Math Fundamentals18 sections · 814 units
Open in Course

Container - Implementation

In pseudocode

Here's the solution:


function maxArea(height)
    left := 0
    right := length of height - 1
    maxArea := 0
    while left < right
        width := right - left
        currentHeight := min(height[left], height[right])
        currentArea := width × currentHeight
        maxArea := max(maxArea, currentArea)
        if height[left] < height[right] then
            left := left + 1
        else
            right := right - 1
    return maxArea

This runs in O(n)O(n) time with O(1)O(1) space. Each pointer moves at most nn times.