Maximum Matching of Players With Trainers

medium two pointers greedy sorting

Problem

Each players[i] is a player's ability and each trainers[j] is a trainer's capacity. A player can match a trainer when ability ≤ capacity. Every player and every trainer is used at most once. Return the maximum number of player–trainer matchings.

Inputplayers = [4,7,9], trainers = [8,2,5,8]
Output2
4 ≤ 8 and 7 ≤ 8 give two matchings; the ability-9 player has no trainer with enough capacity, so 2 is the maximum.

def matchPlayersAndTrainers(players, trainers):
    players.sort()            # weakest players first
    trainers.sort()           # smallest capacities first
    i = j = matched = 0
    while i < len(players) and j < len(trainers):
        if players[i] <= trainers[j]:
            matched += 1      # this trainer can take the player
            i += 1            # advance to the next player
            j += 1            # and consume this trainer
        else:
            j += 1            # trainer too weak, try a bigger one
    return matched
function matchPlayersAndTrainers(players, trainers) {
  players.sort((a, b) => a - b);   // weakest players first
  trainers.sort((a, b) => a - b);  // smallest capacities first
  let i = 0, j = 0, matched = 0;
  while (i < players.length && j < trainers.length) {
    if (players[i] <= trainers[j]) {
      matched++;                   // this trainer can take the player
      i++;                         // advance to the next player
      j++;                         // and consume this trainer
    } else {
      j++;                         // trainer too weak, try a bigger one
    }
  }
  return matched;
}
int matchPlayersAndTrainers(int[] players, int[] trainers) {
    Arrays.sort(players);         // weakest players first
    Arrays.sort(trainers);        // smallest capacities first
    int i = 0, j = 0, matched = 0;
    while (i < players.length && j < trainers.length) {
        if (players[i] <= trainers[j]) {
            matched++;            // this trainer can take the player
            i++;                  // advance to the next player
            j++;                  // and consume this trainer
        } else {
            j++;                  // trainer too weak, try a bigger one
        }
    }
    return matched;
}
int matchPlayersAndTrainers(vector<int>& players, vector<int>& trainers) {
    sort(players.begin(), players.end());     // weakest players first
    sort(trainers.begin(), trainers.end());   // smallest capacities first
    int i = 0, j = 0, matched = 0;
    while (i < players.size() && j < trainers.size()) {
        if (players[i] <= trainers[j]) {
            matched++;            // this trainer can take the player
            i++;                  // advance to the next player
            j++;                  // and consume this trainer
        } else {
            j++;                  // trainer too weak, try a bigger one
        }
    }
    return matched;
}
Time: O(n log n + m log m) Space: O(1) extra (in-place sort)