Fruits Into Baskets II (LeetCode 3477): Solution and Explanation

Easy
Array
Binary Search
Simulation
Segment Tree

Fruits arrive in order, and each takes the leftmost unused basket whose capacity is at least its quantity. Return how many fruit types find no basket. For fruits = [4,2,5] and baskets = [3,5,4] the answer is 1.

Intuition

The placement rule is fully deterministic: the fruit order is fixed and each fruit must take the leftmost basket that fits. There is no choice to optimize, only a process to follow.

The only state that changes is which baskets are taken, so a boolean array is enough to remember it.

With at most 100 fruits and 100 baskets, scanning all baskets for every fruit costs at most 10^4 checks, which is tiny.

Approach: Leftmost-fit simulation

  1. Create used, a boolean array of length n, all false, and set unplaced = 0.
  2. For each fruit quantity f in order, scan j from 0 to n - 1.
  3. Stop at the first j with used[j] false and baskets[j] >= f, and mark used[j] = true.
  4. If the scan ends without a match, increment unplaced.
  5. Return unplaced.

Step-by-step example

fruits = [4,2,5], baskets = [3,5,4]
  1. Fruit 4: basket 0 has capacity 3, too small. Basket 1 has 5, so fruit 4 goes there.
  2. Fruit 2: basket 0 is free with capacity 3 >= 2, so it goes there.
  3. Fruit 5: basket 0 and basket 1 are used, and basket 2 has capacity 4 < 5. No basket fits.
  4. One fruit type is unplaced, so the answer is 1.

Code: Python, Java, C++

1class Solution:
2    def numOfUnplacedFruits(self, fruits: List[int], baskets: List[int]) -> int:
3        used = [False] * len(baskets)
4        unplaced = 0
5        for f in fruits:
6            for j, cap in enumerate(baskets):
7                if not used[j] and cap >= f:
8                    used[j] = True
9                    break
10            else:
11                unplaced += 1
12        return unplaced
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n^2)

Each of the n fruits scans up to n baskets.

Space

O(n)

The used array has one flag per basket.

Other approaches

Segment tree with leftmost descent (time O(n log n), space O(n))

Store the maximum capacity of each range of baskets. To place a fruit, descend toward the leftmost leaf whose capacity is at least the quantity, then set that leaf to 0. This is the intended solution of Fruits Into Baskets III, where n reaches 10^5.

Square-root blocks (time O(n sqrt n), space O(sqrt n))

Split the baskets into blocks of about sqrt(n) and keep each block's maximum free capacity. Skip whole blocks whose maximum is too small, then scan inside the first good block.

Common mistakes

  • Sorting the fruits or the baskets. The rule depends on the original order of both arrays, so sorting changes the answer.
  • Picking the best-fitting basket instead of the leftmost one that fits. That is a different greedy and can place more fruits than the rules allow.
  • Forgetting to mark a basket as used, so two fruit types end up in the same basket.

Frequently asked questions

Why does this problem sit with binary search problems?

The larger version, Fruits Into Baskets III, needs a segment tree that is searched for the leftmost basket with enough capacity, a binary search over the tree. Version II has small limits, so the plain scan is enough.

Is the greedy optimal?

There is nothing to optimize. The statement fixes exactly which basket each fruit takes, so the answer is whatever the simulation produces.

Can a fruit equal to a basket's capacity go in?

Yes. The capacity only needs to be greater than or equal to the quantity.