Maximum Water Trapped Between Two Vertical Lines

Implement maxWaterBetweenLines

Given an array height where height[i] is the height of a vertical line standing at index i, pick two lines that, together with the x-axis, form a container. Return the maximum amount of water that container can hold — min(height[i], height[j]) × (j - i) for the pair you pick. Checking every pair works but wastes time re-deriving what a single pass can rule out: since water level is always capped by the shorter of the two chosen lines, a two-pointer sweep that always advances the shorter side finds the answer in O(n).

Example 1:

Input: height = [3,9,2,6,1,8]

Output: 32

Example 2:

Input: height = [4,4]

Output: 4

Example 3:

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

Output: 5

+ 4 hidden test cases run on Submit.

Constraints:

  • 2 ≤ height.length ≤ 10⁵
  • 0 ≤ height[i] ≤ 10⁴

height =

[3, 9, 2, 6, 1, 8]