Container With Most Water
Part of Practice: Walls & Water
You are given an array height where each value is the height of a vertical line drawn at that index. Pick two lines; together with the x-axis they form an open-topped container. Return the largest amount of water such a container can hold — the width between the lines times the height of the shorter one (water would spill over the shorter side).
The array has at least 2 and up to 100,000 elements, so checking every pair of lines is too slow.
Examples
- Input:
- height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
- Output:
- 49
The lines at index 1 (height 8) and index 8 (height 7) are 7 apart; water level is min(8, 7) = 7, so area = 7 × 7 = 49.
- Input:
- height = [1, 1]
- Output:
- 1
Width 1 times height min(1, 1) = 1.
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.