Loading section...

Container With Most Water: The Squeeze Pattern

Concepts: pySqueezePattern, pyGreedyTwoPointers

Container With Most Water (LeetCode 11) is the other classic medium two-pointer problem. Given an array of heights, find two lines that together with the x-axis form a container that holds the most water. The width is the distance between the two lines. The height is the shorter of the two lines. Maximize area = width x min(height_left, height_right). The brute force checks every pair: O(n^2). Two pointers gives you O(n). Start with pointers at opposite ends (maximum width). At each step, move the pointer with the SHORTER height inward. Why? Because moving the taller pointer can only decrease the area: the width decreases by 1, and the height is bottlenecked by the shorter side which has not changed. Moving the shorter pointer might find a taller line that increases the height enough to co