Trains the technique from
LeetCode 3479Fruits Into Baskets IIIThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A depot has a row of shelf slots, left to right, and baskets[j] is the load rating of slot j in kilograms. Crates arrive one at a time in the order given by fruits, and fruits[i] is the weight of the i-th crate.
Each arriving crate is handled by the same rule: scan the slots from left to right and put the crate in the first still-empty slot whose rating is greater than or equal to the crate's weight. A slot holds at most one crate, and once a crate is in a slot it is never moved. If no still-empty slot is rated high enough, that crate is turned away and the slots are left as they were before it arrived.
Return how many crates end up turned away.
Example 1
The 4 kg crate skips slot 0, rated 3, and lands in slot 1, rated 5. The 2 kg crate then takes slot 0, which is still empty and rated high enough. The 5 kg crate finds only slot 2 free, rated 4, so it is turned away.
Example 2
The first 6 kg crate takes slot 0, whose rating of 6 exactly matches it. The second 6 kg crate passes slot 1, rated 2, and takes slot 2. The 1 kg crate takes slot 1, so no crate is turned away.
Example 3
The first 2 kg crate takes slot 0. Slots 1 and 2 are both rated 1, below 2 kg, so the other two crates are turned away.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def num_of_unplaced_fruits(fruits: list[int], baskets: list[int]) -> int:public int numOfUnplacedFruits(int[] fruits, int[] baskets)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.