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 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
๐Ÿจ Hotel Room Assignment SystemGuests Waiting:3Group A2Group B4Group CAvailable Rooms:5Room 11Room 23Room 3Group A โ†’ Room 1Group B โ†’ Room 34Unassigned!Process: Check rooms left-to-right, assign to first suitable roomResult: 1 group remains unassigned
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

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Only need boolean array to track used baskets

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 0 โ‰ค fruits[i] โ‰ค 109
  • 0 โ‰ค baskets[i] โ‰ค 109
  • fruits.length == baskets.length
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
28.4K Views
Medium Frequency
~18 min Avg. Time
856 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