The problem. You are given an array height where height[i] is the height of a vertical wall at position i. Pick two walls; together with the ground they form a container. Return the most water a container can hold. The water level stops at the shorter wall, so the amount is min(height[i], height[j]) × (j − i).
Input: height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output: 49 walls 1 and 8: min(8, 7) × (8 − 1) = 49
Input: height = [1, 1]
Output: 1Two forces pull against each other: width wants the walls far apart, and height wants both walls tall. Walls between the chosen two don't matter at all — they don't block the water.
Compute the area for every pair (i, j) and keep the largest.
function maxArea(height) { let best = 0; for (let i = 0; i < height.length; i++) { for (let j = i + 1; j < height.length; j++) { best = Math.max(best, Math.min(height[i], height[j]) * (j - i)); } } return best;}Time O(n²), space O(1). With 100,000 walls that's 5 billion pairs.
Every container is limited by its shorter wall. So visit the walls from tallest to shortest. When we reach wall i, every wall visited before it is at least as tall — so pairing i with any of them gives a water level of exactly height[i]. To make that container as big as possible, pair it with the visited wall that is farthest away.
The farthest visited wall is always either the leftmost or the rightmost one, so two numbers — lo and hi — are enough to remember.
function maxArea(height) { // Walls from tallest to shortest. const order = height.map((_, i) => i).sort((a, b) => height[b] - height[a]); let lo = order[0], hi = order[0]; // leftmost / rightmost of the taller walls let best = 0; for (let k = 1; k < order.length; k++) { const i = order[k]; // Every wall seen so far is at least as tall, so the level is height[i]. best = Math.max(best, height[i] * Math.max(i - lo, hi - i)); lo = Math.min(lo, i); hi = Math.max(hi, i); } return best;}Time O(n log n), space O(n). Every pair is still accounted for — each container is considered when its shorter wall is visited — but the sort now does the heavy lifting. Can we avoid sorting altogether?
Start with the widest container: the first and last walls. Any other container is narrower, so it can only win by being taller.
Now look at the shorter of the two walls. Every other container that keeps it is narrower and still capped by that same short wall — so none of them can beat the one we just measured. That wall can never be part of a better answer: drop it by moving its pointer inward. Repeat until the pointers meet.
Watch which wall gets dropped at each step: it's always the shorter one. Try two tall walls at the ends, then a valley.
Time O(n), space O(1). Each step discards one wall for good, so the loop runs at most n − 1 times.
O(n²)Every pair of walls: about n²/2 containers.
O(1)Only the best area and two indexes.
O(n log n)[1, 1] returns 1.[4, 3, 2, 1, 4] returns 16: the widest container wins.[0, 2] returns 0.Two pointers that start at the ends and squeeze inward, each step proving one side can be discarded, also solve 3Sum (next in this chapter) and Trapping Rain Water.
The sort of the walls by height dominates.
O(n)The sorted list of walls with their positions.
O(n)Each step discards one wall for good, so at most n − 1 steps.
O(1)Only the two pointers and the best area.