Container With Most Water
Problem statement
You are given an integer array height of length n. The i-th value represents a vertical pillar from (i, 0) to (i, height[i]).
Choose two pillars that, together with the x-axis, form a container. The water held by pillars at indices i and j is min(height[i], height[j]) * (j - i).
Return the maximum amount of water that any pair of pillars can hold. The container may not be slanted.
Function
maxArea(height: int[]) → intExamples
Example 1
height = [1,8,6,2,5,4,8,3,7]return = 49The pillars at indices 1 and 8 have heights 8 and 7. Their width is 7, so they hold min(8, 7) * 7 = 49 units of water.
Example 2
height = [1,1]return = 1The only pair has width 1 and limiting height 1, so it holds 1 unit.
Example 3
height = [4,3,2,1,4]return = 16The first and last pillars have height 4 and are 4 units apart, so they hold 4 * 4 = 16 units.
Constraints
2 <= height.length <= 10^5.0 <= height[i] <= 10^4.