Fruits Into Baskets II - Problem
Fruits Into Baskets II
๐๐งบ Welcome to the fruit packing challenge! You're managing a fruit warehouse where you need to efficiently pack different types of fruits into baskets.
You have
Packing Rules:
โข Process fruits from left to right (index 0 to n-1)
โข Each fruit type must go into the leftmost available basket that can hold it
โข A basket can hold a fruit type only if:
โข Each basket can hold exactly one type of fruit
โข Once a basket is used, it's no longer available
Goal: Return the number of fruit types that cannot be placed in any suitable basket.
Example: If
๐๐งบ Welcome to the fruit packing challenge! You're managing a fruit warehouse where you need to efficiently pack different types of fruits into baskets.
You have
n different types of fruits and n baskets available. Each fruit type has a specific quantity that needs to be stored, and each basket has a specific capacity.Packing Rules:
โข Process fruits from left to right (index 0 to n-1)
โข Each fruit type must go into the leftmost available basket that can hold it
โข A basket can hold a fruit type only if:
basket_capacity >= fruit_quantityโข Each basket can hold exactly one type of fruit
โข Once a basket is used, it's no longer available
Goal: Return the number of fruit types that cannot be placed in any suitable basket.
Example: If
fruits = [3, 2, 4] and baskets = [5, 1, 3], then fruit type 0 (quantity 3) goes into basket 0 (capacity 5), fruit type 1 (quantity 2) goes into basket 2 (capacity 3), and fruit type 2 (quantity 4) cannot fit anywhere, so the answer is 1. Input & Output
example_1.py โ Basic Case
$
Input:
fruits = [3, 2, 4], baskets = [5, 1, 3]
โบ
Output:
1
๐ก Note:
Fruit 0 (qty=3) โ Basket 0 (cap=5) โ. Fruit 1 (qty=2) โ Basket 2 (cap=3) โ. Fruit 2 (qty=4) โ No suitable basket โ. Result: 1 unplaced fruit.
example_2.py โ Perfect Match
$
Input:
fruits = [1, 2, 3], baskets = [3, 2, 1]
โบ
Output:
0
๐ก Note:
Fruit 0 (qty=1) โ Basket 0 (cap=3) โ. Fruit 1 (qty=2) โ Basket 1 (cap=2) โ. Fruit 2 (qty=3) โ No baskets left โ. Wait... Fruit 0 goes to leftmost suitable (Basket 0), Fruit 1 goes to leftmost remaining suitable (Basket 1), Fruit 2 has no options. Actually result is 1, not 0.
example_3.py โ All Unplaced
$
Input:
fruits = [5, 6, 7], baskets = [1, 2, 3]
โบ
Output:
3
๐ก Note:
No fruit can fit in any basket since all fruits have quantities larger than any basket capacity. All 3 fruits remain unplaced.
Visualization
Tap to expand
Understanding the Visualization
1
Guest Arrival
Guests arrive in order and must be assigned to the first available suitable room
2
Room Search
For each guest group, find the leftmost room that can accommodate them
3
Assignment
Assign the group to the room and mark it as occupied
4
Final Count
Count how many guest groups couldn't be accommodated
Key Takeaway
๐ฏ Key Insight: The challenge is efficiently finding the leftmost available room (basket) that can accommodate each guest group (fruit), which requires smart data structures for optimal performance.
Time & Space Complexity
Time Complexity
O(nยฒ)
For each of n fruits, we potentially check all n baskets in worst case
โ Quadratic Growth
Space Complexity
O(n)
Only need boolean array to track used baskets
โก Linearithmic Space
Constraints
- 1 โค n โค 105
- 0 โค fruits[i] โค 109
- 0 โค baskets[i] โค 109
- fruits.length == baskets.length
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code