code.davidloor.com
MMedium· two-pointers· array

Container With Most Water

Container With Most Water

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 that holds the most water. Return the maximum amount of water the container can store.

You may not slant the container.

Example

Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The lines at indices 1 and 8 (heights 8 and 7) form a container with width 7 and minimum height 7, giving area 49.

Constraints

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

Entry — maxArea

Grading — exact over 4 tests

Stuck? Browse algorithmic patterns → or read the Python / JS foundations.
Awaiting first run
Reference solution unlocks after a run & 60 s of work.