#K80807. Container With Most Water
Container With Most Water
Container With Most Water
Given an array of non-negative integers heights
where each element represents the height of a vertical line drawn at that position, your task is to find two lines that together with the x-axis form a container such that the container holds the maximum amount of water.
The container's area can be computed by:
$$\text{Area} = (j-i) \times \min(h_i, h_j),$$where i
and j
are the indices of the two lines (with i < j
), and h_i
and h_j
are their respective heights.
You are required to implement a function that computes this maximum area given the input list of heights. The solution should use an efficient approach (for example, a two-pointer technique) to handle large inputs.
inputFormat
The first line of input contains an integer n representing the number of vertical lines. The second line contains n space-separated integers representing the heights of the lines.
outputFormat
Output a single integer, which is the maximum amount of water that can be contained.
## sample9
1 8 6 2 5 4 8 3 7
49