Back to ArraysContainer With Most Water
Medium

Container With Most Water

MediumMust DoTwo Pointers

Given an array of heights representing vertical lines, find two lines that, together with the x-axis, hold the most water, and return that maximum area.

Examples

Example 1

Input:
height = [1,8,6,2,5,4,8,3,7]
Output:
49

Explanation: The lines at index 1 (height 8) and index 8 (height 7) form the best container: min(8, 7) * (8 - 1) = 49.

Example 2

Input:
height = [1,1]
Output:
1

Explanation: The minimal valid input — two lines of height 1, one unit apart, hold exactly 1 unit of water.

Constraints

  • 2 <= height.length <= 10^5
  • 0 <= height[i] <= 10^4

Follow-up

Why is it never correct to move the taller boundary inward first? Try to justify it without referring to the code.

Loading editor…

Code execution is coming soon.