Problem · Array
Container With Most Water
Learn this problemProblem statement
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i-th line are (i, 0) and (i, height[i]).
Find two lines that, together with the x-axis, form a container so that the container holds the most water.
Return the maximum amount of water a container can store.
You may not slant the container.
Function
maxArea(height: int[]) → intExamples
Example 1
height = [1,8,6,2,5,4,8,3,7]return = 49The lines at indices 1 and 8 have heights 8 and 7. The width is 7, so the area is min(8, 7) * 7 = 49.
Example 2
height = [1,1]return = 1The only pair forms a container of width 1 and height 1.
Constraints
2 <= height.length <= 10^5.0 <= height[i] <= 10^4.