Skip to content
Decoder

Container With Most Water

Medium

Problem

You are given a list height where height[i] is the height of a vertical wall at position i. Pick two walls; together with the ground they hold water up to the shorter wall. The amount of water is min(height[i], height[j]) * (j - i). Return the most water any pair can hold.

Example 1

Input
height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output
49
Why
walls at 1 and 8: min(8, 7) * 7

Example 2

Input
height = [1, 1]
Output
1

Example 3

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

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.