Maximum Matching of Players With Trainers (LeetCode 2410): Solution and Explanation

Medium
Two Pointers
Greedy
Array
Sorting

Given player abilities and trainer capacities, pair each player with at most one trainer whose capacity is at least the player's ability, and return the largest number of pairs.

Intuition

Think about the weakest player. Every trainer that can handle anyone can handle this player, so they are the easiest to place. The question is which trainer to spend on them.

Spend the smallest trainer that is strong enough. Any bigger trainer is more useful later, and if some best answer used a bigger one, swapping in the smaller one keeps every pair valid.

In the other direction, a trainer weaker than the weakest waiting player is weaker than every waiting player, so it can be thrown away for good.

After sorting both lists these two rules become a single sweep with one pointer in each list.

Approach: Sort and greedy two pointers

  1. Sort players and trainers in increasing order.
  2. Set i = 0 (weakest waiting player) and j = 0 (weakest unused trainer).
  3. While both pointers are in range: if players[i] <= trainers[j], match them and advance i. In either case advance j.
  4. Return i, the number of players that got a trainer.

Step-by-step example

players = [4,7,9], trainers = [8,2,5,8]
  1. After sorting: players = [4,7,9], trainers = [2,5,8,8].
  2. Player 4 vs trainer 2: too weak, discard the trainer (j = 1).
  3. Player 4 vs trainer 5: match (i = 1, j = 2).
  4. Player 7 vs trainer 8: match (i = 2, j = 3).
  5. Player 9 vs trainer 8: too weak, discard (j = 4). Trainers are used up, so the answer is i = 2.

Code: Python, Java, C++

1class Solution:
2    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
3        players.sort()
4        trainers.sort()
5        i = j = 0
6        while i < len(players) and j < len(trainers):
7            if players[i] <= trainers[j]:
8                i += 1
9            j += 1
10        return i
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n log n + m log m)

Sorting dominates. The sweep afterwards moves j through every trainer once and i at most once per player.

Space

O(1)

The arrays are sorted in place and only two pointers are kept (the sort itself may use O(log n) stack).

Other approaches

Strongest player first (time O(n log n + m log m), space O(1))

Walk both sorted lists from the top: if the strongest trainer can handle the strongest remaining player, pair them, otherwise give up on that player. It reaches the same count.

Bipartite matching (time O(E * sqrt(V)), space O(E))

Build the full compatibility graph and run a maximum matching algorithm. Correct, but it ignores the simple threshold structure and is far too slow for 10^5 elements.

Common mistakes

  • Advancing i when the trainer is too weak. A weak trainer says nothing bad about the player; it is the trainer that must be discarded.
  • Forgetting to sort one of the arrays. The greedy argument needs both lists in increasing order.
  • Using a strict comparison. A player can match a trainer whose capacity equals their ability, so the test is <=.
  • Sorting numbers as strings in JavaScript. sort() with no comparator puts 10 before 9; pass (a, b) => a - b.

Frequently asked questions

Why is giving the weakest player the smallest workable trainer optimal?

Take any best matching. If it gives that player a bigger trainer, or leaves the small trainer unused or paired with someone else, swapping the two trainers keeps every pair valid, because the other player is at least as strong as the weakest one and the swapped-in trainer is at least as big. So some best matching agrees with the greedy choice.

Is this the same as Assign Cookies?

Yes, the structure is identical: children's greed factors play the players and cookie sizes play the trainers. The same sort-and-sweep solves both.

Can the answer exceed the shorter list?

No. Each pair uses one player and one trainer, so the answer is at most min(len(players), len(trainers)), and the loop stops as soon as either list runs out.