Ctrl K

Leftmost Locker That Fits

Medium
A pickup point has a row of n lockers, and lockers[j] is the capacity of locker j. There are n parcels to store, and parcels[i] is the size of parcel i. The parcels are handled in order, starting from the first. Each one goes into the leftmost free locker whose capacity is at least the parcel's size, and a locker holds only one parcel. If no free locker fits, the parcel is left out and the next one is handled. Return how many parcels are left out.
Example 1
parcels=[5, 2, 6], lockers=[3, 6, 2] → 1

Parcel 5 does not fit locker 3 and goes into the second one (6). Parcel 2 takes the first locker (3). Only the locker of size 2 is left for parcel 6, so it stays out.

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

Parcel 1 takes the first locker even though the third would also fit, because the leftmost free one is chosen. Then a 4 goes into the second locker and the last 4 has nowhere to go.

Example 3
parcels=[2, 2], lockers=[2, 9] → 0

Both parcels fit, one per locker.

Constraints
  • 1 ≤ n = len(parcels) = len(lockers) ≤ 10⁵
  • 1 ≤ parcels[i], lockers[j] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsOnes Outnumber Zeros MediumRank of an Arrangement MediumSmaller Values to the Right HardWindow Sums Within a Band HardPairs With a Steep Drop Hard
loading Python…
Input
parcels=[5, 2, 6], lockers=[3, 6, 2]
Expected
1

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