Maximum Matching of Players With Trainers - Problem

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 player
  • trainers[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

example_1.py — Basic Matching
$ Input: players = [4,7,9], trainers = [8,2,5,8]
Output: 2
💡 Note: Player with ability 4 can match with trainer capacity 5 or 8. Player with ability 7 can match with trainer capacity 8. Player with ability 9 cannot match with any trainer (all have capacity < 9). Maximum matches = 2.
example_2.py — Perfect Matching
$ Input: players = [1,1,1], trainers = [10,10,10]
Output: 3
💡 Note: All players have ability 1 and all trainers have capacity 10. Since 1 ≤ 10, every player can match with any trainer. Maximum matches = 3.
example_3.py — No Matches Possible
$ Input: players = [10,20,30], trainers = [1,2,3]
Output: 0
💡 Note: Every player's ability is greater than every trainer's capacity. No valid matches can be made. Result = 0.

Visualization

Tap to expand
Sports Academy Matching StrategyBefore Sorting:479Players:8258Trainers:After Sorting:479Players:2588Trainers:Matching Process:4vs2✗ (4 > 2)4vs5✓ Match!7vs8✓ Match!9vs8✗ (9 > 8)Why Greedy Works:• Matching weak player with strong trainer wastes the trainer's potential• Always use the minimum sufficient trainer• This preserves strong trainers for strong playersFinal Result: 2 successful matches!Valid match (player ≤ trainer)Invalid (player > trainer)
Understanding the Visualization
1
Sort by Skill Level
Line up players and trainers by their abilities - weakest to strongest. This helps us make optimal decisions.
2
Start with Weakest Player
Always try to match the weakest available player first. They have the most limited options.
3
Find Cheapest Suitable Trainer
For each player, find the weakest trainer who can still handle them. This preserves stronger trainers for harder cases.
4
Greedy Choice Property
This greedy strategy works because using a stronger trainer for a weaker player never improves the total - it only wastes resources.
Key Takeaway
🎯 Key Insight: The greedy approach works because using a minimal sufficient trainer for each player preserves stronger trainers for players who truly need them, maximizing total matches.

Time & Space Complexity

Time Complexity
⏱️
O(n log n + m log m)

Dominated by sorting both arrays where n=players.length and m=trainers.length

n
2n
Linearithmic
Space Complexity
O(1)

Only using constant extra space for pointers and counter (excluding sorting space)

n
2n
Linear 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
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.4K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen