Show problems as
Classic problem #3477EasyArrayBinary SearchSegment TreeSimulationOrdered Set

Fruits Into Baskets II

Real-world version: Crates That Find No Bin
Tracked in this browser only
Read Fruits Into Baskets II on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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