Match as many players as possible with trainers whose capacity is at least the player's requirement.
You are given two integer arrays: players and trainers.
Each player has a minimum training capacity requirement, and each trainer has a capacity value. A player can be matched with a trainer only if the trainer's capacity is greater than or equal to the player's requirement. Each player and each trainer can be used at most once.
Return the maximum number of matches you can form.
The goal is to pair players and trainers so that the number of valid pairs is as large as possible.
Input Format
- Two integer arrays
playersandtrainers. players[i]is the requirement of the -th player.trainers[j]is the capacity of the -th trainer.
Output Format
- Return a single integer: the maximum number of valid player-trainer matches.
Constraints
- Each player and trainer may be used at most once.
- A match is valid only if
trainerCapacity >= playerRequirement. - Arrays may be unsorted.
Example 1
Input
players = [4, 7, 9] trainers = [8, 2, 5, 8]
Output
2
Explanation
After sorting, players = [4, 7, 9] and trainers = [2, 5, 8, 8].
- Match player 4 with trainer 5.
- Match player 7 with trainer 8. Player 9 cannot be matched. So the maximum number of matches is 2.
Example 2
Input
players = [1, 1, 1] trainers = [1, 2]
Output
2
Explanation
Two players can be matched: one with trainer 1 and one with trainer 2. One player remains unmatched.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.