2410. Maximum Matching of Players With Trainers
MediumView on LeetCode
Time: O(n log n)
Space: O(1)
Advertisement
Intuition
Match players to trainers optimally: each player needs a trainer with capacity >= player skill. Maximize matches.
Algorithm
- 1Sort both. Two pointers: match smallest player to smallest sufficient trainer.
Common Pitfalls
- •Greedy: sort both, use two pointers. If trainer >= player: match and advance both. Else: advance trainer.
2410.cs
C#
// Approach: Sort both arrays; greedy two-pointer — match player to smallest capable trainer.
// Time: O(n log n) Space: O(1)
public class Solution
{
public int MatchPlayersAndTrainers(int[] players, int[] trainers)
{
int ans = 0;
Array.Sort(players);
Array.Sort(trainers);
for (int i = 0; i < trainers.Length; ++i)
{
if (players[ans] <= trainers[i] && ++ans == players.Length)
return ans;
}
return ans;
}
}Advertisement
Was this solution helpful?