Menu
CoddyTech

Container With Most Water

You get a list height of non-negative integers. Line i is a vertical wall of height height[i] standing at position i. Any two lines form a container with the ground, and it holds as much water as the shorter line's height times the distance between the two lines. The other lines do not get in the way. Return the most water a single pair of lines can hold.

Function

maxArea(height: integer-array) → integer
heightinteger-array
the heights of the lines at positions 0, 1, 2 and so on
Returnsinteger
the most water two lines can hold

Constraints

  • 2 ≤ height.length ≤ 104
  • 0 ≤ height[i] ≤ 104
  • The answer is at most 108, so it fits in a 32-bit integer.

Examples

Input
height = [3, 7, 2, 5, 4, 7, 3, 6]
Output
36
Explanation
The lines at positions 1 and 7 have heights 7 and 6 and stand 6 apart, so they hold 6 × 6 = 36. The two tallest lines, the 7s at positions 1 and 5, hold only 7 × 4 = 28, and the outer pair holds 3 × 7 = 21.

lock icon+15 hidden tests on Submit

challenge icon

Follow-up

Here the lines between the two you pick are ignored. If every line were a solid bar instead, how much water would collect between all of them? Can you compute that in O(n) as well?

Reset code
def maxArea(height):
    # Write code here
Test cases

Case 1

Case 2

Input

height = [3, 7, 2, 5, 4, 7, 3, 6]

Expected

36