All problems
0963EasyArrayBinary SearchSegment TreeSimulationOrdered Set

Crates That Find No Bin

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3477Fruits Into Baskets II

This 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.

Crate sizes are given as fruits and bin capacities as baskets, both of the same length.

The crates are dealt with in the order listed. Each crate goes into the leftmost bin not yet used whose capacity is at least the crate's size; a bin holds at most one crate. A crate with no such bin left is unplaced.

Return how many crates end up unplaced.

Examples

Example 1

Input
fruits = [14, 3, 27, 9], baskets = [10, 30, 5, 20]
Output
1

The crate of 14 takes the bin of 30, since the bin of 10 is too small. The crate of 3 takes the bin of 10. The crate of 27 finds nothing big enough left, and the crate of 9 takes the bin of 20.

Example 2

Input
fruits = [3, 1], baskets = [1, 3]
Output
0

The crate of 3 takes the second bin, and the crate of 1 takes the first, which was passed over as too small the first time.

Example 3

Input
fruits = [7, 7, 7], baskets = [6, 6, 6]
Output
3

Every bin is smaller than every crate, so nothing can be placed.

Constraints

  • fruits.length == baskets.length
  • 1 <= fruits.length <= 100
  • 1 <= fruits[i] <= 1000
  • 1 <= baskets[i] <= 1000

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def num_of_unplaced_fruits(fruits: list[int], baskets: list[int]) -> int:
Java
public int numOfUnplacedFruits(int[] fruits, int[] baskets)
September 7
Apply