KatInterview

11. Container With Most Water

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to find the maximum area of a container that can be formed by two vertical lines from a given array height. The i-th line has coordinates (i, 0) and (i, height[i]). The area of a container formed by lines at indices i and j (with i < j) is calculated as min(height[i], height[j]) * (j - i). A brute-force approach would be to check every possible pair of lines, which results in an O(n²) time complexity, too slow for the given constraints. A more efficient approach is needed.

Solution

Container With Most Water in Python

class Solution:
    def maxArea(self, height: List[int]) -> int:
        left = 0
        right = len(height) - 1
        max_area = 0

        while left < right:
            # Calculate the width of the container.
            width = right - left
            # Calculate the height of the container, limited by the shorter line.
            h = min(height[left], height[right])
            # Calculate the current area.
            current_area = width * h
            # Update the maximum area found so far.
            max_area = max(max_area, current_area)

            # Move the pointer that points to the shorter line.
            # This is the greedy part: to potentially find a larger area, 
            # we must increase the height, as the width is decreasing.
            # We move the shorter line's pointer inward, hoping to find a taller line.
            if height[left] < height[right]:
                left += 1
            else:
                right -= 1
        
        return max_area

Container With Most Water in Java

class Solution {
    public int maxArea(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int maxArea = 0;

        while (left < right) {
            // Calculate the width of the container
            int width = right - left;
            // Calculate the height of the container, limited by the shorter line
            int h = Math.min(height[left], height[right]);
            // Calculate the current area
            int currentArea = width * h;
            // Update the maximum area found so far
            maxArea = Math.max(maxArea, currentArea);

            // Move the pointer that points to the shorter line.
            // This is because moving the pointer of the taller line will always 
            // result in a smaller or equal area, as the width decreases and 
            // the height is still limited by the shorter line.
            if (height[left] < height[right]) {
                left++;
            } else {
                right--;
            }
        }
        return maxArea;
    }
}

Container With Most Water in C++

class Solution {
public:
    int maxArea(vector<int>& height) {
        int left = 0;
        int right = height.size() - 1;
        int max_area = 0;

        while (left < right) {
            // Calculate the width of the container
            int width = right - left;
            // Calculate the height of the container, limited by the shorter line
            int h = min(height[left], height[right]);
            // Calculate the current area
            int current_area = width * h;
            // Update the maximum area found so far
            max_area = max(max_area, current_area);

            // Move the pointer that points to the shorter line. By doing this,
            // we discard the shorter line and explore the possibility of finding a taller
            // line to form a larger container, despite the reduced width.
            if (height[left] < height[right]) {
                left++;
            } else {
                right--;
            }
        }
        return max_area;
    }
};

Container With Most Water in TypeScript

function maxArea(height: number[]): number {
    let left = 0;
    let right = height.length - 1;
    let maxArea = 0;

    while (left < right) {
        // Calculate the width of the container.
        const width = right - left;
        // Calculate the height of the container, limited by the shorter line.
        const h = Math.min(height[left], height[right]);
        // Calculate the current area.
        const currentArea = width * h;
        // Update the maximum area found so far.
        maxArea = Math.max(maxArea, currentArea);

        // Move the pointer of the shorter line inward.
        // The rationale is that moving the taller line's pointer can't lead to a larger area.
        // The new width would be smaller, and the new height would be at most the height of the shorter line.
        // By moving the shorter line's pointer, we explore possibilities of finding a taller line
        // which might compensate for the reduced width.
        if (height[left] < height[right]) {
            left++;
        } else {
            right--;
        }
    }

    return maxArea;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.