← Back

Container With Most Water

Same converging scan, but move the shorter wall to chase a bigger area.

Medium LeetCode ↗ 1 / 1
func maxArea(height []int) int {

}
func maxArea(height []int) int {
    // Micropattern: widest container first, then shrink smartly.
}
func maxArea(height []int) int {
    // Micropattern: widest container first, then shrink smartly.
    left, right := 0, len(height)-1
    best := 0
}
func maxArea(height []int) int {
    // Micropattern: widest container first, then shrink smartly.
    left, right := 0, len(height)-1
    best := 0

    for left < right {

    }
}
func maxArea(height []int) int {
    // Micropattern: widest container first, then shrink smartly.
    left, right := 0, len(height)-1
    best := 0

    for left < right {
        // Area is limited by the shorter of the two walls.
    }
}
func maxArea(height []int) int {
    // Micropattern: widest container first, then shrink smartly.
    left, right := 0, len(height)-1
    best := 0

    for left < right {
        // Area is limited by the shorter of the two walls.
        h := min(height[left], height[right])
        best = max(best, h*(right-left))
    }
}
func maxArea(height []int) int {
    // Micropattern: widest container first, then shrink smartly.
    left, right := 0, len(height)-1
    best := 0

    for left < right {
        // Area is limited by the shorter of the two walls.
        h := min(height[left], height[right])
        best = max(best, h*(right-left))

        // Moving the taller wall can never help — move the shorter one.
    }
}
func maxArea(height []int) int {
    // Micropattern: widest container first, then shrink smartly.
    left, right := 0, len(height)-1
    best := 0

    for left < right {
        // Area is limited by the shorter of the two walls.
        h := min(height[left], height[right])
        best = max(best, h*(right-left))

        // Moving the taller wall can never help — move the shorter one.
        if height[left] < height[right] {
            left++
        } else {
            right--
        }
    }
}
func maxArea(height []int) int {
    // Micropattern: widest container first, then shrink smartly.
    left, right := 0, len(height)-1
    best := 0

    for left < right {
        // Area is limited by the shorter of the two walls.
        h := min(height[left], height[right])
        best = max(best, h*(right-left))

        // Moving the taller wall can never help — move the shorter one.
        if height[left] < height[right] {
            left++
        } else {
            right--
        }
    }

    return best // O(n) time, O(1) space
}

Tap the left half to go back, the right half to go forward.

Arrow keys or space to navigate · Esc to exit.