> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Fruit Into Baskets Python Solution with Tests

> Tested Python solution for LeetCode 904 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 904, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/fruit-into-basket/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 904   # by problem number
lcpy gen -s fruit_into_basket   # by problem name
```

## Problem

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 \<code>fruits\</code> where \<code>fruits\[i]\</code> is the \<strong>type\</strong> of fruit the \<code>i\<sup>th\</sup>\</code> tree produces. You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:\</p>

\<ul>
\<li>You only have \<strong>two\</strong> baskets, and each basket can only hold a \<strong>single type\</strong> of fruit. There is no limit on the amount of fruit each basket can hold.\</li>
\<li>Starting from any tree of your choice, you must pick exactly \<strong>one fruit\</strong> from \<strong>every\</strong> tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets.\</li>
\<li>Once you get to a tree, the fruit cannot jump back to the tree 1. (For example, if you pick fruit 1 from tree 2, you cannot pick fruit 1 from tree 1.)\</li>
\<li>Given the integer array \<code>fruits\</code>, return \<em>the \<strong>maximum\</strong> number of fruits you can pick.\</em>\</li>
\</ul>

### Examples

```
Input: fruits = [1,2,1]
Output: 3
Explanation: We can pick from all 3 trees.
```

```
Input: fruits = [0,1,2,2]
Output: 3
Explanation: We can pick from trees [1,2,2].
If we had started from the first tree, we would only pick from trees [0,1].
```

```
Input: fruits = [1,2,3,2,2]
Output: 4
Explanation: We can pick from trees [2,3,2,2].
If we had started from the first tree, we would only pick from trees [1,2].
```

### Constraints

* 1 \<= fruits.length \<= 10^5
* 0 \<= fruits\[i] \< fruits.length

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/fruit_into_basket/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/fruit_into_basket/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import defaultdict


class Solution:
    # Time: O(n)
    # Space: O(1)
    def total_fruit(self, fruits: list[int]) -> int:
        counts: defaultdict[int, int] = defaultdict(int)
        left = 0
        best = 0
        for right, fruit in enumerate(fruits):
            counts[fruit] += 1
            while len(counts) > 2:
                left_fruit = fruits[left]
                counts[left_fruit] -= 1
                if counts[left_fruit] == 0:
                    del counts[left_fruit]
                left += 1
            best = max(best, right - left + 1)
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.