Container With Most Water - Leetcode Solution


💡 Step-by-Step Thought Process

  1. Understand the problem: Find two lines that form a container with the maximum water area, given an array of heights.
  2. Initialize left pointer to 0 and right pointer to the array length minus 1.
  3. Initialize max_area to 0 to track the maximum area found.
  4. While left is less than right, calculate the width as right minus left.
  5. Calculate the height as the minimum of height[left] and height[right].
  6. Compute the area as width times height and update max_area if the computed area is larger.
  7. If height[left] is less than height[right], increment left; otherwise, decrement right.
  8. Return max_area after the loop.

Code Solution


                

                

                

                

Detailed Explanation

Understanding the Problem: Container With Most Water

The “Container With Most Water” problem involves an array of non-negative integers, where each value represents the height of a vertical line drawn on the x-axis. The task is to find the two lines that, together with the x-axis, form a container that holds the most water. You are to return the maximum area of water that such a container can contain.

Example:

  • Input: [1,8,6,2,5,4,8,3,7]
  • Output: 49
The lines at indices 1 and 8 form the container with area = min(8,7) × (8−1) = 7 × 7 = 49.

Why This Problem Matters

This problem is a classic in the use of the two-pointer technique and is frequently asked in coding interviews. It teaches how to maximize values under constraints, how to analyze trade-offs between width and height, and how to approach optimization without checking every possibility.

Brute Force Idea (Inefficient)

The naive approach would involve checking the area formed by every possible pair of lines. This means two nested loops and a time complexity of O(n²), which is inefficient for large inputs.

Optimal Solution: Two-Pointer Technique

Since the area is determined by the shorter of two lines and the distance between them, we can use two pointers to efficiently find the maximum area. This method starts with the widest possible container (the outermost two lines) and moves inward to find better options.

Steps:

  1. Initialize two pointers:
    • left = 0 (start of array)
    • right = height.length - 1 (end of array)
  2. Initialize max_area = 0.
  3. While left < right:
    • Calculate width = right - left.
    • Compute min_height = min(height[left], height[right]).
    • Calculate area: area = width × min_height.
    • Update max_area if area is larger.
    • Move the pointer pointing to the shorter line inward:
      • If height[left] < height[right], increment left.
      • Else, decrement right.
  4. Return max_area.

Example Walkthrough

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

  • Start with left = 0 and right = 8 → height[0] = 1, height[8] = 7
  • Area = min(1,7) × (8−0) = 1 × 8 = 8
  • Since height[0] is shorter, move left pointer → now left = 1
  • Now height[1] = 8, height[8] = 7 → min = 7, width = 7 → area = 49
  • Repeat until all possible pairs are scanned via pointers
  • Max area found: 49

Time and Space Complexity

Time Complexity: O(n), where n is the number of lines. Each line is visited at most once by either pointer.
Space Complexity: O(1), since we only use a few variables for tracking indices and maximum area.

Edge Cases to Consider

  • Array with less than 2 elements → no container can be formed
  • All heights are equal → max area depends only on width
  • Decreasing or increasing height patterns → still works correctly due to pointer logic

Conclusion

The “Container With Most Water” problem is a clever application of the two-pointer technique. By reducing the problem from quadratic to linear time, this solution demonstrates how leveraging input structure and greedy decision-making leads to highly efficient algorithms.

Get Personalized Support at AlgoMap Bootcamp 💡