Container With Most Water
Container With Most Water
Section titled “Container With Most Water”
Medium
Day 2 • Striver Blind 75
📌 Problem Overview
Section titled “📌 Problem Overview”You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i-th line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container that holds the most water. Return the maximum amount of water a container can store.
You may not slant the container.
Examples & Constraints
Section titled “Examples & Constraints”Example 1:
- Input:
height = [1,8,6,2,5,4,8,3,7] - Output:
49
Example 2:
- Input:
height = [1,1] - Output:
1
Constraints:
n == height.length2 ≤ n ≤ 10⁵0 ≤ height[i] ≤ 10⁴
💡 Approach & Intuition
Section titled “💡 Approach & Intuition”Container With Most Water tests the greedy two-pointer insight that moving the shorter line inward is the only move that can improve area.
🎯 Pattern Recognition
Section titled “🎯 Pattern Recognition”Pattern: Greedy Two Pointers
Start with the widest container (both ends). The area is capped by the shorter line, so moving the taller line inward can never help — always move the shorter one.
📊 Step-by-Step Execution (Mermaid Diagram)
Section titled “📊 Step-by-Step Execution (Mermaid Diagram)”graph LR L["Left Pointer (L)"] --> Array["Input Array / String"] R["Right Pointer (R)"] --> Array Array --> Condition{"Check Window Condition"} Condition -- "Expand R" --> R Condition -- "Shrink L" --> L Condition -- "Valid State" --> Max["Update Max / Subarray Result"]🐢 Brute Force Solution
Section titled “🐢 Brute Force Solution”function maxArea(height) { let max = 0; for (let i = 0; i < height.length; i++) { for (let j = i + 1; j < height.length; j++) { max = Math.max(max, Math.min(height[i], height[j]) * (j - i)); } } return max;}- Time Complexity:
O(n²) - Space Complexity:
O(1) - Explanation: Check every pair of lines.
⚡ Optimized Solution
Section titled “⚡ Optimized Solution”function maxArea(height) { let left = 0, right = height.length - 1, max = 0; while (left < right) { const area = Math.min(height[left], height[right]) * (right - left); max = Math.max(max, area); if (height[left] < height[right]) left++; else right--; } return max;}- Time Complexity:
O(n) - Space Complexity:
O(1) - Explanation: Two pointers shrinking from the widest container, always moving the shorter line.
🐾 Step-by-Step Walkthrough
Section titled “🐾 Step-by-Step Walkthrough”- Initialize State: Setup necessary pointers, dynamic programming arrays, or hash maps.
- Iterate & Evaluate: Process the input according to the boundary conditions.
- Update & Return: Compute the optimal answer and return early or at termination.
🎙️ FAANG Interview Pitch
Section titled “🎙️ FAANG Interview Pitch”- Start with brute force checking all pairs
- Start pointers at widest container instead
- Prove moving the taller line can never increase area
- Always move the shorter line inward until pointers meet
💡 Progressive Hints
Section titled “💡 Progressive Hints”- Start with pointers at both ends for maximum width.
- Area = min(height[left], height[right]) * (right - left).
- Always move the pointer at the shorter line inward.