Container With Most Water

MediumTwo PointersGreedy

You are given an array height of n non-negative integers. Each value is the height of a vertical line drawn at that index on the x-axis.

Choose two lines that, together with the x-axis, form a container. Return the maximum amount of water a container can hold. Water can't be slanted: its level is the height of the shorter of the two lines, and its width is the distance between their indices.

Constraints

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

Examples

Example 1

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

Lines at index 1 (height 8) and index 8 (height 7) are 7 apart. The water level is the shorter line, 7, so the area is 7 × 7 = 49.

Example 2

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

Example 3

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

The two 4s are 4 apart: 4 × 4 = 16.

JavaScript you'll use: Numbers, Math and integer tricks Arrays: create, read, update

/**
 * @param {number[]} height
 * @return {number}
 */
function maxArea(height) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.