Updated about 20 hours ago | GitHub
On this page

Container With Most Water

Problem Highlights

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • Is it possible for us to have negative heights?
    • No, the lowest height possible in this problem is 0.
  • What is the minimum number of heights?
    • The minimum number of heights is 2.
  • What is the time and space complexity considerations?
    • Let’s try for a runtime of O(n) and space of O(1)
HAPPY CASE
Input: [1,8,6,2,5,4,8,3,7]
Output: 49 (The max area is between index 1 and 8 with a 
minimum height of 7, giving us a container of 7 by 7, which is 49)

Input: [1,1]
Output: 1

EDGE CASE
Input:[0,0]
Output: 0

2: M-atch

Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.

For Array, common solution patterns include:

  • Sort
    • The array should not be altered, the index of each height represents the width of the rectangle, which is important for us in determining the maximum area of a rectangle.
  • Two pointer solutions (left and right pointer variables):
    • Intuition, the area is the width times the shorter of the two heights. Start with the left and right pointers at the two ends, which gives the max width. The shorter line caps the height of every container it is part of, so pairing it with any line between the pointers gives a narrower container that is no taller, and so no larger area. That means we can safely discard the shorter line and move its pointer inward. We do not find the maximum area at every width, but we never discard the pair that gives the overall maximum.
  • Storing the elements of the array in a HashMap or a Set
    • Sort values into a hashmap might not be very useful here
  • Traversing the array with a sliding window
    • A sliding window is useful for selecting a window, but that is not what we are trying to do here.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Use the two pointer solution. Start at the max width, and at each step move the pointer at the shorter line inward, because that line cannot form a larger area with any narrower pair. Track the largest area seen along the way.

1. Create largest range, by creating a left and right pointer
2. At every part of the range calculate the current area at this width and check against our max area
    a. Reduce the range/width by moving the pointer at the shorter height
3. Return max area found

⚠️ Common Mistakes

  • A less optimized approach would be to check every height against every other height. A n^2 approach.

4: I-mplement

Implement the code to solve the algorithm.

class Solution:
    def maxArea(self, height: List[int]) -> int:
        # Create largest range, by creating a left and right pointer
        l, r = 0, len(height) - 1
        maxArea = 0

        # At every part of the range calculate the current area at this width and check against our max area
        while l < r:
            width = r - l
            minHeight = min(height[l], height[r])
            currArea = minHeight * width
            maxArea = max(maxArea, currArea)

            # Reduce the range/width by moving the pointer at the shorter height
            if height[l] < height[r]:
                l += 1
            else:
                r -= 1

        # Return max area found
        return maxArea
class Solution {
    public int maxArea(int[] height) {
        // Create largest range, by creating a left and right pointer
        int left = 0;
        int right = height.length - 1;
        int max = 0;

        // At every part of the range calculate the current area at this width and check against our max area
        while(left < right){
            int w = right - left;
            int h = Math.min(height[left], height[right]);
            int area = h * w;
            max = Math.max(max, area);

            //Reduce the range/width by moving the pointer at the shorter height (both pointers when the heights are equal)
            if(height[left] < height[right]) left++;
            else if(height[left] > height[right]) right--;
            else {
                left++;
                right--;
            }
        }
        
        //Return max area found
        return max;
    }
}

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code’s variables along the way.

  • Trace through your code with an input to check for the expected output
  • Catch possible edge cases and off-by-one errors

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

Assume N represents the number of heights in array.

  • Time Complexity: O(N), because we need to check the entire range of heights once.
  • Space Complexity: O(1), because we only use a constant number of variables to hold our area information.