Problem 337155 · easy · Phase 03 Linear Management & Searching

Match Porters to Parcels

greedy · sorting · two pointers

porters[i] is the maximum weight porter i can carry and parcels[j] is the weight of parcel j. Every porter carries at most one parcel and a parcel goes to at most one porter, who must be strong enough for it. Return the maximum number of parcels that can be delivered.

Examples

Input:  porters = [3, 5, 8], parcels = [4, 9, 2]
Output: 2
Explanation: Parcel 2 goes to porter 3 and parcel 4 to porter 5; nobody can carry 9.
Input:  porters = [1, 1], parcels = [2, 2]
Output: 0

Constraints

  • 0 <= len(porters), len(parcels) <= 10**5, all values positive integers.
  • Target complexity: O(n log n + m log m).

Goals

  • Sort both lists so that a two-pointer walk finds a maximum matching
  • Justify why giving the lightest parcel to the weakest capable porter is safe
Starting Python…