[LeetCode] Maximum Matching of Players With Trainers

2410. Maximum Matching of Players With Trainers

You are given a 0-indexed integer array players, where players[i] represents the ability of the ith player. You are also given a 0-indexed integer array trainers, where trainers[j] represents the training capacity of the jth trainer.

The ith player can match with the jth trainer if the player’s ability is less than or equal to the trainer’s training capacity. Additionally, the ith player can be matched with at most one trainer, and the jth trainer can be matched with at most one player.

Return the maximum number of matchings between players and trainers that satisfy these conditions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int matchPlayersAndTrainers(vector<int>& A, vector<int>& B) {
sort(begin(A), end(A));
sort(begin(B), end(B));
int res = 0;
while(A.size() and B.size()) {
if(A.back() <= B.back()) {
A.pop_back();
B.pop_back();
res++;
} else A.pop_back();
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/18/PS/LeetCode/maximum-matching-of-players-with-trainers/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.