Container With Most Water

Step 5: Container With Most Water

Implement max_area(heights). Each element is the height of a vertical line at that index. Pick two lines that, together with the x-axis, form a container holding the most water, and return that maximum area.

The area between indices i and j is min(heights[i], heights[j]) multiplied by (j - i). Start pointers at both ends and always move the pointer at the shorter line inward, since that is the only move that can increase the area.

Hints
Starter code
def two_sum_sorted(nums, target):
    i, j = 0, len(nums) - 1
    while i < j:
        s = nums[i] + nums[j]
        if s == target:
            return (i, j)
        elif s < target:
            i += 1
        else:
            j -= 1
    return None


def is_palindrome(s):
    i, j = 0, len(s) - 1
    while i < j:
        while i < j and not s[i].isalnum():
            i += 1
        while i < j and not s[j].isalnum():
            j -= 1
        if s[i].lower() != s[j].lower():
            return False
        i += 1
        j -= 1
    return True


def reverse_in_place(arr):
    i, j = 0, len(arr) - 1
    while i < j:
        arr[i], arr[j] = arr[j], arr[i]
        i += 1
        j -= 1
    return arr


def remove_duplicates(sorted_nums):
    if not sorted_nums:
        return 0
    slow = 0
    for fast in range(1, len(sorted_nums)):
        if sorted_nums[fast] != sorted_nums[slow]:
            slow += 1
            sorted_nums[slow] = sorted_nums[fast]
    return slow + 1


def max_area(heights):
    """Return the maximum water area between two lines using two pointers."""
    # TODO: start at both ends and move the shorter line inward
    pass
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.