AlgoLounge

Container With Most Water

medium

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, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.

Examples

Example 1

Input:  height = [1,8,6,2,5,4,8,3,7]
Output: 49

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example 2

Input:  height = [1,1]
Output: 1

The two lines are at positions 0 and 1 with heights 1 and 1, forming a container with area 1.

Constraints

  • n == height.length
  • 2 <= n <= 10⁵
  • 0 <= height[i] <= 10⁴
TypeScript
Loading editor…
List of test cases (10)
Case 1
height = [1,8,6,2,5,4,8,3,7]
Case 2
height = [1,1]
Case 3
height = [4,3,2,1,4]
Case 4
height = [1,2,1]
Case 5
height = [2,1]
Case 6
height = [1,3,2,5,25,24,5]
Case 7
height = [5,5,5,5]
Case 8
height = [1,0,0,0,0,0,0,2,2]
Case 9
height = [10,9,8,7,6,5,4,3,2,1]
Case 10
height = [2,3,4,5,18,17,6]