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, heights 7 and 6, stand 6 apart and hold 6 × 6 = 36. The two tallest lines, the 7s at positions 1 and 5, hold only 7 × 4 = 28.
challenge icon

Follow-up

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

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

Test #1

Test #2

Argument

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

Expected Output

36