Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed integer array players, where players[i] represents the ability of the ith player. You are also given a 0-indexed integer array trainers, where trainers[j] represents the training capacity of the jth trainer.
The ith player can match with the jth trainer if the player's ability is less than or equal to the trainer's training capacity. Additionally, the ith player can be matched with at most one trainer, and the jth trainer can be matched with at most one player.
Return the maximum number of matchings between players and trainers that satisfy these conditions.
Example 1:
Input: players = [4,7,9], trainers = [8,2,5,8] Output: 2 Explanation: One of the ways we can form two matchings is as follows: - players[0] can be matched with trainers[0] since 4 <= 8. - players[1] can be matched with trainers[3] since 7 <= 8. It can be proven that 2 is the maximum number of matchings that can be formed.
Example 2:
Input: players = [1,1,1], trainers = [10] Output: 1 Explanation: The trainer can be matched with any of the 3 players. Each player can only be matched with one trainer, so the maximum answer is 1.
Constraints:
1 <= players.length, trainers.length <= 1051 <= players[i], trainers[j] <= 109Note: This question is the same as 445: Assign Cookies.
Problem summary: You are given a 0-indexed integer array players, where players[i] represents the ability of the ith player. You are also given a 0-indexed integer array trainers, where trainers[j] represents the training capacity of the jth trainer. The ith player can match with the jth trainer if the player's ability is less than or equal to the trainer's training capacity. Additionally, the ith player can be matched with at most one trainer, and the jth trainer can be matched with at most one player. Return the maximum number of matchings between players and trainers that satisfy these conditions.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Greedy
[4,7,9] [8,2,5,8]
[1,1,1] [10]
most-profit-assigning-work)long-pressed-name)interval-list-intersections)largest-merge-of-two-strings)maximum-number-of-tasks-you-can-assign)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2410: Maximum Matching of Players With Trainers
class Solution {
public int matchPlayersAndTrainers(int[] players, int[] trainers) {
Arrays.sort(players);
Arrays.sort(trainers);
int m = players.length, n = trainers.length;
for (int i = 0, j = 0; i < m; ++i, ++j) {
while (j < n && trainers[j] < players[i]) {
++j;
}
if (j == n) {
return i;
}
}
return m;
}
}
// Accepted solution for LeetCode #2410: Maximum Matching of Players With Trainers
func matchPlayersAndTrainers(players []int, trainers []int) int {
sort.Ints(players)
sort.Ints(trainers)
m, n := len(players), len(trainers)
for i, j := 0, 0; i < m; i, j = i+1, j+1 {
for j < n && trainers[j] < players[i] {
j++
}
if j == n {
return i
}
}
return m
}
# Accepted solution for LeetCode #2410: Maximum Matching of Players With Trainers
class Solution:
def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
players.sort()
trainers.sort()
j, n = 0, len(trainers)
for i, p in enumerate(players):
while j < n and trainers[j] < p:
j += 1
if j == n:
return i
j += 1
return len(players)
// Accepted solution for LeetCode #2410: Maximum Matching of Players With Trainers
impl Solution {
pub fn match_players_and_trainers(mut players: Vec<i32>, mut trainers: Vec<i32>) -> i32 {
players.sort();
trainers.sort();
let mut j = 0;
let n = trainers.len();
for (i, &p) in players.iter().enumerate() {
while j < n && trainers[j] < p {
j += 1;
}
if j == n {
return i as i32;
}
j += 1;
}
players.len() as i32
}
}
// Accepted solution for LeetCode #2410: Maximum Matching of Players With Trainers
function matchPlayersAndTrainers(players: number[], trainers: number[]): number {
players.sort((a, b) => a - b);
trainers.sort((a, b) => a - b);
const [m, n] = [players.length, trainers.length];
for (let i = 0, j = 0; i < m; ++i, ++j) {
while (j < n && trainers[j] < players[i]) {
++j;
}
if (j === n) {
return i;
}
}
return m;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.