2Brute2Best
🔥0

Container With Most Water

Medium·LeetCode #11 Pro OnlyPattern Transfer
LeetCode
01

Problem Statement

You are given n vertical lines. Line i stands at position i and has height height[i].

Pick two lines. Together with the x-axis they form a container. The water inside can only rise as high as the shorter of the two lines, and its width is the distance between them.

Return the most water a container can hold: the largest value of min(height[i], height[j]) × (j − i).

The lines in between do not block anything, and you may not tilt the container.

Input

An integer array height of length n.

Output

One integer: the maximum area over all pairs of lines.

Example 1
Input: height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output: 49

Pick line 1 (height 8) and line 8 (height 7). The water rises to 7 and the width is 7, so the area is 7 × 7 = 49.

Example 2
Input: height = [1, 1]
Output: 1

Only one pair: height 1, width 1.

Constraints
  • • 2 ≤ n ≤ 10^5
  • • 0 ≤ height[i] ≤ 10^4
  • • The largest area is at most 10^4 × 10^5 = 10^9, which fits in an int.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“pick two lines”
→ The answer is a pair (i, j) → a pair problem, so think about two pointers.
“width is j − i”
→ The widest pair is the two ends. Starting there and moving inward only ever shrinks the width.
“limited by the shorter line”
→ This gives the rule for which pointer to move: the shorter line can't do better, so drop it.
03

Approach Complexity Ladder

Brute Force — try every pairO(N²)O(1)

Measure the area of all n·(n−1)/2 pairs

Optimal — two pointers, move the shorter lineO(N)O(1)

Start at both ends; measure, then drop the shorter line

Brute Force Intuition

A container is just a pair of lines (i, j). Try every pair, compute min(height[i], height[j]) × (j − i), and keep the largest.

Recognition rule for this sub-pattern:

Start at both ends of a sorted (or symmetric) structure. Each step, move the side that cannot do better.
Remember this · Two Pointers
Trigger
Sorted array, pairs/triplets with a target, comparing from both ends, compacting an array in place.
Think
If I compare the elements at two positions, can I safely discard one side?
Key question
Which pointer moves, and why can the skipped elements never be the answer?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.