Medium · Arrays

Trap most water

Given an array height of n ≥ 2 non-negative integers, where height[i] is a vertical line at position i, return the most water a container formed by two of the lines and the x-axis can hold, i.e. the maximum over i < j of (j − i) × min(height[i], height[j]).

Examples

Example 1

[1,8,6,2,5,4,8,3]

Output: best 40

Example 2

[1,2,4,3]

Output: best 4

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Arrays problems