Container With Most Water

Arrays & Two-Pointers · medium

Given an array `heights` where `heights[i]` is the height of a vertical line at index `i`, find two lines that together with the x-axis form a container that holds the most water. Return the maximum water volume. Volume = min(heights[i], heights[j]) × (j − i).

Hints
  • Use two pointers starting at each end.
  • Move the pointer on the shorter side inward each iteration.

Learning resources

Loading...

Run your code to see results.