Maximum Matching of Players With Trainers
Problem Statement
Each player has an ability, and each trainer has a capacity: the highest ability they can train.
A player can be matched with a trainer only if the player's ability is at most the trainer's capacity. Each player can be matched with at most one trainer, and each trainer can take at most one player.
Make as many matches as you can. Return that number.
players, the ability of each player, and trainers, the capacity of each trainer.
One integer: the largest number of playerβtrainer matches.
Match the ability-4 player with a capacity-8 trainer, and the ability-7 player with the other capacity-8 trainer. The ability-9 player is too strong for every trainer.
One trainer can take only one player, so the answer is 1.
- β’ 1 β€ players.length, trainers.length β€ 10^5
- β’ 1 β€ players[i], trainers[j] β€ 10^9
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Backtrack over the players: each one gets no trainer, or any free trainer that is strong enough
Sort both lists. Walk the trainers from weakest; each one takes the weakest waiting player it can train
Go through the players in order. For each one, try no trainer, and try each unused trainer whose capacity is at least the player's ability. Keep the largest number of matches over all these choices.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.