Largest Water Container

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write maxArea(heights): heights[i] is the height of a vertical line at position i. Any two lines, with the ground, make a container that holds water up to the shorter line, so lines at positions i and j hold (j minus i) times the smaller height. Return the most water any pair can hold, or 0 with fewer than two lines. For example, maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7]) is 49, from the lines of height 8 and 7, seven apart. The checks include 100,000 lines, so trying every pair is too slow.

Your code must pass

  • Finds the pair of lines holding the most water
  • Fills only up to the shorter line
  • Returns 0 with fewer than two lines
  • Is fast enough for 100,000 lines