Container With Most Water

Medium#11towards each other

You get the heights of walls that stand in a row, one step apart. Pick two of them so that the most water fits between them: the distance between the walls times the lower one. Our example: heights [2, 6, 3, 5, 1, 4, 7, 3] — the answer is 30, the walls 6 and 7.

input
height: the heights of vertical lines standing one unit apart.
returns
The most water a container of two of the lines and the ground can hold: the largest (j − i) · min(height[i], height[j]).
constraints
  • 2 ≤ height.length ≤ 3000
  • 0 ≤ height[i] ≤ 10⁴
our 12 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[]} height
 * @return {number}
 */
function maxArea(height) {

}
Ctrl ↵
format
reset

saved in this browser

12 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more two pointers problems

Pattern “Two pointers” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.