Imagine you're running a sports academy where you need to match players with trainers for optimal training sessions. Each player has a specific ability level, and each trainer has a training capacity that represents the maximum ability level they can effectively coach.
Given two arrays:
players[i]- the ability of the i-th playertrainers[j]- the training capacity of the j-th trainer
A player can only be matched with a trainer if the player's ability ≤ trainer's capacity. Additionally, each player can have at most one trainer, and each trainer can coach at most one player.
Goal: Find the maximum number of valid player-trainer pairs you can create.
Example: If players = [4,7,9] and trainers = [8,2,5,8], you can match:
• Player with ability 4 → Trainer with capacity 8
• Player with ability 7 → Trainer with capacity 8
• Player with ability 9 → Cannot be matched (no trainer with capacity ≥ 9)
Result: 2 matches
Input & Output
Visualization
Time & Space Complexity
Dominated by sorting both arrays where n=players.length and m=trainers.length
Only using constant extra space for pointers and counter (excluding sorting space)
Constraints
- 1 ≤ players.length, trainers.length ≤ 105
- 1 ≤ players[i], trainers[j] ≤ 109
- Each player can match with at most one trainer
- Each trainer can match with at most one player