ntree.ai

Container With Most Water

mediumArraysTwo pointersGreedy

You are given an integer array height, where each entry is the height of a vertical line standing at that index. Two lines and the axis between them form a container. Find the two lines that hold the most water.

Example 1:

Input:  height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output: 49
Explanation: the lines at indices 1 and 8 are 8 and 7 tall, 7 apart: min(8, 7) * 7 == 49.

Example 2:

Input:  height = [1, 1]
Output: 1
Explanation: the only pair, one apart, both 1 tall.

Constraints:

  • 2 <= len(height) <= 10^5
  • 0 <= height[i] <= 10^4