2410. Maximum Matching of Players With Trainers
MediumView on LeetCode
Time: O(n log n)
Space: O(1)
Problem Overview
Match players to trainers optimally: each player needs a trainer with capacity >= player skill.
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;
}
}Was this solution helpful?
Related Problems
- 4. Median of Two Sorted Arrays(Hard)
- 11. Container With Most Water(Medium)
- 15. 3Sum(Medium)
- 16. 3Sum Closest(Medium)
- 19. Remove Nth Node From End of List(Medium)
- 26. Remove Duplicates from Sorted Array(Easy)