WorkspaceProblem lab
ALGORITHM VISUALIZER

Container With Most Water

Opening the interactive workspace…

Understanding Container With Most Water

Return the largest area between two vertical lines whose heights are given by heights.

How it works

The shorter boundary limits the area, so move that boundary inward.

Start at the two outermost lines. Measure width times the shorter height. Move the shorter boundary and keep the best area.

Try an example

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

Output: 49

The shorter boundary limits the area, so move that boundary inward.

Constraints

  • 0–64 integer heights between 0 and 1,000,000.
  • Fewer than two lines returns 0.

Use the simulator

Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.

Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.

Related concepts and complexity