Fruits Into Baskets II (LeetCode 3477): Solution and Explanation
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
- Create
used, a boolean array of length n, all false, and setunplaced = 0. - For each fruit quantity
fin order, scanjfrom 0 to n - 1. - Stop at the first
jwithused[j]false andbaskets[j] >= f, and markused[j] = true. - If the scan ends without a match, increment
unplaced. - Return
unplaced.
Step-by-step example
fruits = [4,2,5], baskets = [3,5,4]
- Fruit 4: basket 0 has capacity 3, too small. Basket 1 has 5, so fruit 4 goes there.
- Fruit 2: basket 0 is free with capacity 3 >= 2, so it goes there.
- Fruit 5: basket 0 and basket 1 are used, and basket 2 has capacity 4 < 5. No basket fits.
- 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 unplaced1class Solution {
2 public int numOfUnplacedFruits(int[] fruits, int[] baskets) {
3 int n = baskets.length;
4 boolean[] used = new boolean[n];
5 int unplaced = 0;
6 for (int f : fruits) {
7 boolean placed = false;
8 for (int j = 0; j < n; j++) {
9 if (!used[j] && baskets[j] >= f) {
10 used[j] = true;
11 placed = true;
12 break;
13 }
14 }
15 if (!placed) unplaced++;
16 }
17 return unplaced;
18 }
19}1class Solution {
2public:
3 int numOfUnplacedFruits(vector<int>& fruits, vector<int>& baskets) {
4 int n = baskets.size();
5 vector<bool> used(n, false);
6 int unplaced = 0;
7 for (int f : fruits) {
8 bool placed = false;
9 for (int j = 0; j < n; j++) {
10 if (!used[j] && baskets[j] >= f) {
11 used[j] = true;
12 placed = true;
13 break;
14 }
15 }
16 if (!placed) unplaced++;
17 }
18 return unplaced;
19 }
20};Time and space complexity
O(n^2)
Each of the n fruits scans up to n baskets.
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.