Skip to main content
impol.dev

Editorial

Solution walkthrough

Review the explanation, then compare the reference implementation.

Video explanation

Solution

Intuition

The placement rule is prescribed: process fruits in their original order and choose the leftmost unused basket with enough capacity. We are not free to reorder fruits or choose a different basket to improve later placements. The small input allows direct simulation.

Approach

Keep a set of used basket indices. For each fruit quantity, scan baskets from left to right, skipping baskets already used or too small. On the first eligible basket, mark its index used and stop the scan.

If no basket was found, increment the unplaced count. Return that count after all fruit types are processed. Tracking indices rather than capacities distinguishes separate baskets with equal sizes. Restarting the basket scan from the left for every fruit enforces the required leftmost choice, including smaller baskets skipped by earlier, larger fruit quantities.

class Solution: def numOfUnplacedFruits(self, fruits: List[int], baskets: List[int]) -> int: n = len(baskets) used_b = set() res = 0 for fruit in fruits: used_f = 0 for bi in range(n): if bi in used_b or baskets[bi] < fruit: continue used_b.add(bi) used_f = 1 break if not used_f: res += 1 return res

Complexity analysis

Let f = fruits.length and b = baskets.length. Time: O(f * b). Auxiliary space: O(b) for used basket indices.