Ctrl K

Parcels Into Lockers

Easy
A parcel station has a row of lockers whose capacities are listed in lockers, from left to right. A courier loads parcels strictly in the order of parcels, where parcels[i] is the size of the i-th parcel. Each parcel goes into the **leftmost free** locker whose capacity is at least the parcel's size. A locker holds only one parcel. If no free locker fits, the parcel stays with the courier and they move on to the next one. Return how many parcels end up not placed.
Example 1
parcels=[6, 2, 9], lockers=[3, 7, 8] → 1

Parcel 6 goes into locker 7 (locker 3 is too small), parcel 2 goes into locker 3, and parcel 9 fits nowhere, since only locker 8 is left. One parcel is left over.

Example 2
parcels=[1, 5], lockers=[5, 2] → 1

Parcel 1 takes the leftmost locker that fits, which is 5. Parcel 5 is left with locker 2 only and does not fit, even though another assignment would have placed both.

Example 3
parcels=[4, 4, 4], lockers=[4, 10, 4] → 0
Constraints
  • 1 ≤ len(parcels), len(lockers) ≤ 100
  • 1 ≤ parcels[i], lockers[j] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSmaller Values to the Right HardWindow Sums Within a Band HardPairs With a Steep Drop HardAND of a Stretch Near the Goal HardLast Step With a Lit Run Medium
loading Python…
Input
parcels=[6, 2, 9], lockers=[3, 7, 8]
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 110 tests on the server, hidden ones included.