Leetcode!
  • LeetCode!
  • My personal guide to Leetcode
  • Array
    • 11. Container With Most Water
    • 15. 3Sum
    • 219. Contains Duplicate II
    • 238. Product of Array Except Self
    • 245. Shortest Word Distance III
    • 392. Is Subsequence
    • 442. Find All Duplicates in an Array
    • 561. Array Partition I
    • 661. Image Smoother
    • 697. Degree of an Array
    • 723. Candy Crush
    • 832. Flipping an Image
  • Backtracking
    • 294. Flip Game II
    • 401. Binary Watch
    • 1079. Letter Tile Possibilities
  • Binary Search
    • 744. Find Smallest Letter Greater Than Target
    • 852. Peak Index in a Mountain Array
  • String
    • 6. ZigZag Conversion
    • 17. Letter Combinations of a Phone Number
    • 38. Count and Say
    • 521. Longest Uncommon Subsequence I
    • 544. Output Contest Matches
    • 937. Reorder Data in Log Files
  • Design
    • 251. Flatten 2D Vector
    • 281. Zigzag Iterator
  • Hash Table
    • 1. Two Sum
    • 508. Most Frequent Subtree Sum
    • 884. Uncommon Words from Two Sentences
    • 734. Sentence Similarity
  • Linked List
    • 2. Add Two Numbers
  • Unsorted
    • 917. Reverse Only Letters
  • Math
    • 43. Multiply Strings
    • 462. Minimum Moves to Equal Array Elements II
    • 553. Optimal Division
    • 800. Similar RGB Color
  • Greedy
    • 406. Queue Reconstruction by Height
    • 861. Score After Flipping Matrix
  • Tree
    • 701. Insert into a Binary Search Tree
    • 897. Increasing Order Search Tree
  • Divide and Conquer
    • 53. Maximum Subarray
  • Dynamic Programming
    • 62. Unique Paths
    • 63. Unique Paths II
    • 64. Minimum Path Sum
    • 120. Triangle
    • 198. House Robber
    • 213. House Robber II
    • 303. Range Sum Query - Immutable
    • 518. Coin Change 2
    • 750. Number Of Corner Rectangles
    • 337. House Robber III
  • Depth first search
    • 841. Keys and Rooms
  • Trie
    • 677. Map Sum Pairs
  • Two pointer
    • 42. Trapping Rain Water
Powered by GitBook
On this page
  • Approach 1: Brute Force
  • Approach 2: Two Ends Approach
  1. Array

11. Container With Most Water

The problem requires us to find the maximum area between vertical lines of different heights at different separations.

Approach 1: Brute Force

As always, brute forcing is the first solution that strikes. For each of the lines look at all the other lines to see if they enclose an area greater than what we have already seen. Although not expected to pass, I still went ahead and tried this solution just in case the test cases weren't extremely large.

def maxArea(self, height):     
    """     
    :type height: List[int]     
    :rtype: int     
    """     
    lh = len(height)     
    max_vol = -1     
    for i in range(lh):         
        for j in range(i+1, lh):             
            if i == j:                 
                continue             
            if (j-i)*min(height[i],height[j]) > max_vol:                 
                max_vol = (j-i)*min(height[i],height[j])     
    return max_vol

As expected, the test cases were large enough that the time limit exceeded on test case 41/49

Time Complexity: O(n²)

Space Complexity: O(1)

Approach 2: Two Ends Approach

This one took some serious help to come up with. Drawing diagrams on a whiteboard didn't seem to help since I could not get rid of the second loop

The fix to that problem was to start at the ends of the array and then each time move the end with the shorter height to the next one.

This approach ensured that the array was only looked at once and reduced the runtime by a factor of n

def maxArea(self, height):
    """
    https://leetcode.com/problems/container-with-most-water/description/
    :type height: List[int]
    :rtype: int
    """
    max_area = area = 0
    left, right = 0, len(height) - 1
    while left < right:
        l, r = height[left], height[right]
        if l < r:
            area = (right - left) * l
            while height[left] <= l:
                left += 1
        else:
            area = (right - left) * r
            while height[right] <= r and right:
                right -= 1
        if area > max_area:
            max_area = area
    return max_area

As expected this solution was accepted.

Time Complexity: O(n) (Only one pass)

Space Complexity: O(1)

PreviousMy personal guide to LeetcodeNext15. 3Sum

Last updated 6 years ago