Find two vertical lines that form a container holding the maximum amount of water.
Container With Most Water
You are given an array of non-negative integers where each value represents the height of a vertical line drawn at that index. Choose any two lines together with the x-axis to form a container.
Return the maximum amount of water the container can hold.
The water contained by two lines at positions and is determined by:
- the distance between them, and
- the shorter of the two heights.
So the area is:
Find the largest possible area over all valid pairs.
Input Format
- An integer array
heightof lengthn height[i]is the height of the vertical line at indexi
Output Format
- Return a single integer: the maximum container area
Constraints
- Use 64-bit arithmetic if your language may overflow on large products
Example 1
Input
height = [1,8,6,2,5,4,8,3,7]
Output
49
Explanation
The best pair is the lines at indices 1 and 8 with heights 8 and 7. The width is 7, so the area is 7 × 7 = 49.
Example 2
Input
height = [1,1]
Output
1
Explanation
Only one pair exists. The width is 1 and the limiting height is 1, so the area is 1.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.