Fruit Into Baskets - Problem

Imagine you're visiting a fruit orchard where trees are arranged in a single row from left to right. Each tree produces one type of fruit, represented by an integer in the array fruits where fruits[i] is the fruit type of the i-th tree.

You want to maximize your fruit collection, but there are strict rules:

  • ๐Ÿงบ You have exactly two baskets
  • ๐ŸŽ Each basket can only hold one type of fruit
  • ๐Ÿ“ฆ Each basket has unlimited capacity for its fruit type
  • โžก๏ธ You must start at any tree and move only to the right
  • ๐Ÿ›‘ You must stop when you encounter a third fruit type

Goal: Find the maximum number of fruits you can collect following these rules.

Example: For [1,2,1], you can collect all 3 fruits (types 1 and 2 fit in your two baskets).

Input & Output

example_1.py โ€” Basic Case
$ Input: [1,2,1]
โ€บ Output: 3
๐Ÿ’ก Note: We can collect all fruits: start at index 0, collect fruit type 1, then 2, then 1 again. Both types fit in our two baskets, so we get 3 fruits total.
example_2.py โ€” Multiple Types
$ Input: [0,1,2,2]
โ€บ Output: 3
๐Ÿ’ก Note: We can start at index 1 and collect [1,2,2]. This gives us 3 fruits with only 2 types (1 and 2). Starting at index 0 would only give us [0,1] = 2 fruits before hitting the third type.
example_3.py โ€” Single Type
$ Input: [1,2,3,2,2]
โ€บ Output: 4
๐Ÿ’ก Note: The optimal collection is [2,3,2,2] starting from index 1, which gives us 4 fruits with types 2 and 3. Any other starting position gives fewer fruits.

Visualization

Tap to expand
AppleOrangeAppleOrangeGrapeBasket 1: AppleBasket 2: OrangeSTOP!Third fruit typeMaximum Collection: 4 fruits[Apple, Orange, Apple, Orange]Valid Collection Window
Understanding the Visualization
1
Start collecting
Begin with empty baskets and start collecting fruits from left to right
2
Expand collection
Keep collecting fruits as long as you have at most 2 different types
3
Handle third type
When you encounter a third fruit type, you must abandon some fruits from the left to make room
4
Track maximum
Throughout the process, remember the largest collection you've achieved
Key Takeaway
๐ŸŽฏ Key Insight: This is essentially finding the longest contiguous subarray with at most 2 distinct elements - a perfect use case for the sliding window technique!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each element is visited at most twice (once by right pointer, once by left pointer), making it linear time

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

HashMap stores at most 3 fruit types (temporarily when contracting window), which is constant space

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค fruits.length โ‰ค 105
  • 0 โ‰ค fruits[i] < fruits.length
  • Follow-up: Can you solve this in O(n) time?
Asked in
Amazon 42 Google 38 Meta 31 Microsoft 25
34.4K Views
High Frequency
~18 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen