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 ith line are (i, 0) and (i, height[i]).

Find two lines that together with the x-axis form a container, that can hold the most water. Return the maximum amount of water the container can store.

The container sides must be vertical; you may not slant the container.

Example 1
  #         #
  #         #   #
  # #       #   #
  # #   #   #   #
  # #   # # #   #
  # #   # # # # #
  # # # # # # # #
# # # # # # # # #
1 8 6 2 5 4 8 3 7
Inputheight = [1,8,6,2,5,4,8,3,7]
Output49
The lines at indices 1 and 8 have heights 8 and 7, giving an area of min(8, 7) * (8 - 1) = 49.
Example 2
# #
1 1
Inputheight = [1,1]
Output1
The only possible container uses both lines and holds min(1, 1) * 1 = 1 unit of water.

Constraints

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

Asked at 57 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate