InferenceAI

Enter at least two characters to search.

Start free

Widest Water Container

mediumTwo Pointers25 min25 XP

heights[i] is the height of a vertical wall at position ii. Choose two walls; the water they hold is

area=(j−i)×min⁡(hi,hj)\text{area} = (j - i) \times \min(h_i, h_j)

Return the maximum possible area.

Requirements

  • Run in O(n)O(n) time.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[1,8,6,2,5,4,8,3,7]]
TASKwidest_water_container()
EXPECTED OUTPUT49

Start with the input values shown in this example.

Example 1 of 3

Input: heights = [1,8,6,2,5,4,8,3,7]

Output: 49

Explanation: classic

Input: heights = [1,1]

Output: 1

Explanation: two walls

Input: heights = [4,3,2,1,4]

Output: 16

Explanation: outer walls best

Hint 1

Start with the widest pair and move the shorter wall inward. Why can moving the taller wall never help?

Topics

DSATwo PointersPython