Container with Most Water - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

Problem

Given a height array of length n (non-negative integers), pick two lines that, together with the x-axis, form a container holding the maximum water, and return the area of that container.
For indices i and j (0 ≤ i < j < n), the area is (j − i) × min(height[i], height[j]).

Pointers & State

  • left — starts at 0, points to the left line.
  • right — starts at n − 1, points to the right line.
  • maxWater — running maximum area found so far.

Key Insight

Area = width × min(height[left], height[right]).
Moving either pointer always shrinks the width.
The shorter (or equal) side is a bottleneck for minimum height — keeping it cannot increase the minimum height, and since the width does not expand back (it only shrinks at each move), it cannot yield a larger area.
👉 Therefore, always move the pointer on the shorter (or equal) side to try to replace it with a taller line.

Algorithm (Step-by-Step, matching the code)

  1. Initialize
    • left = 0, right = n − 1.
    • maxWater = 0.
  2. While left < right:
    1. Measure the current container
      width = right − leftminHeight = min(height[left], height[right])area = width × minHeight.
    2. Update maxWater if area is larger.
    3. Move the shorter side (symmetric reasoning):
      • If height[left] < height[right] → advance left by one.
      • Else (the right side is shorter or equal) → retreat right by one.
      Rationale: the shorter (or equal) side is the bottleneck for height; width is shrinking anyway, so keeping the shorter side cannot improve area. Moving it is the only chance to find a taller minimum height.
  3. Return maxWater after the loop terminates.

Correctness & Termination

  • Each step strictly shrinks the window by moving exactly one pointer; therefore the loop while (left < right) must terminate in at most n − 1 iterations.
  • A larger area is only possible if the minimum height increases; since width shrinks, keeping the shorter side cannot help.
  • Always moving the shorter side systematically considers exactly those candidates that could raise the minimum height, ensuring the maximum is found.

Time Complexity

O(n)
We use a two-pointer scan across the array. Each step moves exactly one pointer (left or right), strictly shrinking the window. Therefore, the loop runs for at most n − 1 iterations, giving overall O(n) time.

Space Complexity

O(1)
We only store a few variables: left, right, and maxWater. No extra data structures are used beyond the input array, so the additional space usage is constant.

Visucodize editorial solution titled JS Solution for LeetCode's Container with Most Water coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/container-with-most-water.