Container With Most Water

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

Given n non-negative integers height[0], height[1], ..., height[n-1], where each represents the height of a vertical line drawn at index i, find two lines that together with the x-axis form a container that holds the most water.

Return the maximum amount of water the container can store.

Note: You may not slant the container.

Examples

Example 1:

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

Output: 49

Explanation: The lines at index 1 (height 8) and index 8 (height 7) form a container of width 7 and height 7, giving area = 7 * 7 = 49.

Example 2:

Input: height = [1,1]

Output: 1

Explanation: The only container possible has width 1 and height 1, giving area = 1 * 1 = 1.

Example 3:

Input: height = [4,3,2,1,4]

Output: 16

Explanation: The lines at index 0 (height 4) and index 4 (height 4) form a container of width 4 and height 4, giving area = 4 * 4 = 16.

Constraints

  • n == height.length
  • 2 <= n <= 10⁵
  • 0 <= height[i] <= 10⁴
Source: Two Pointers pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle