LeetCode 11 Container With Most Water - Problem Statement

The problem

You're given an array where height[i] represents a vertical line at position i. Find two lines that, together with the x-axis, form a container holding the most water.

For height = [1, 8, 6, 2, 5, 4, 8, 3, 7], the answer is 49. Lines at positions 1 and 8 (heights 8 and 7) form a container with width 7, so area = min(8, 7) × 7 = 49.

Checking all pairs takes O(n2)O(n^2). How can you find the maximum in a single pass?

Constraints: 2n1052 \le n \le 10^5, heights from 00 to 10410^4.