Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Fruit Into Baskets

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "904" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow: You only have two baskets, and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice, you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits, return the maximum number of fruits you can pick. Example 1: Input: fruits = [1,2,1] Output: 3 Explanation: We can pick from all 3 trees. Example 2: Input: fruits = [0,1,2,2] Output: 3 Explanation: We can pick from trees [1,2,2]. If we had started at the first tree, we would only pick from trees [0,1]. Example 3: Input: fruits = [1,2,3,2,2] Output: 4 Explanation: We can pick from trees [2,3,2,2]. If we had started at the first tree, we would only pick from trees [1,2]. Constraints: 1 <= fruits.length <= 105 0 <= fruits[i] < fruits.length

Explanation

  • Sliding Window: Use a sliding window approach to maintain a window of fruit trees that contain at most two types of fruit.
    • Expanding and Shrinking: Expand the window to the right, adding trees. If the window contains more than two types of fruit, shrink the window from the left until it contains at most two types again.
    • Maximum Length: Keep track of the maximum length of the window that satisfies the condition (at most two types of fruit).
  • Runtime Complexity: O(n), where n is the length of the fruits array. Storage Complexity: O(1). We use a constant amount of extra space.

Code

    def totalFruit(fruits):
    """
    Calculates the maximum number of fruits that can be picked from a row of trees,
    given the constraint of having two baskets that can each hold only one type of fruit.

    Args:
        fruits (list[int]): An array representing the type of fruit each tree produces.

    Returns:
        int: The maximum number of fruits that can be picked.
    """

    max_fruits = 0
    left = 0
    fruit_types = {}  # Store the counts of each fruit type within the window

    for right in range(len(fruits)):
        # Add the current fruit to the window
        fruit_types[fruits[right]] = fruit_types.get(fruits[right], 0) + 1

        # Shrink the window if it contains more than two types of fruit
        while len(fruit_types) > 2:
            fruit_types[fruits[left]] -= 1
            if fruit_types[fruits[left]] == 0:
                del fruit_types[fruits[left]]
            left += 1

        # Update the maximum number of fruits picked
        max_fruits = max(max_fruits, right - left + 1)

    return max_fruits

More from this blog

C

Chatmagic blog

2894 posts