Skip to main content

Maximum Matching of Players With Trainers - Solution & Explanation

MediumArrayTwo PointersGreedySorting18 min readAsked at: Amazon, Meta, Google
Practice this problem

Problem Statement

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 <= 105
  • 1 <= players[i], trainers[j] <= 109

 

Note: This question is the same as 445: Assign Cookies.

Approach Overview

Problem Overview: You have two arrays: players representing player ability and trainers representing trainer capacity. A trainer can train one player only if trainer >= player. Each trainer can be used once. The task is to maximize the number of valid player‑trainer matches.

Approach 1: Two-Pointer Technique after Sorting (O(n log n) time, O(1) extra space)

Sort both arrays, then greedily match the smallest available player with the smallest trainer that can handle them. Use two pointers: one for players and one for trainers. If the current trainer capacity is greater than or equal to the player's ability, record a match and move both pointers. Otherwise move only the trainer pointer to search for a stronger trainer. Sorting ensures you never waste a high-capacity trainer on a weak player when a smaller trainer could work. The algorithm performs a single linear scan after sorting, making it efficient and easy to implement using concepts from sorting, two pointers, and greedy algorithms.

Approach 2: Greedy Matching with Ordered Sets (O(n log n) time, O(n) space)

Insert all trainer capacities into an ordered structure such as a multiset (C++) or bisect/sorted container style structure in Python. Iterate through players and find the smallest trainer whose capacity is at least the player's ability using a lower_bound-style lookup. If such a trainer exists, match them and remove that trainer from the set so it cannot be reused. Each lookup and removal takes O(log n), and you perform it for each player. This approach is useful when you want explicit control over matching or when processing players in a custom order.

The key greedy insight is that weaker players should consume the weakest valid trainers. Assigning a large-capacity trainer too early can block a future player who actually needs that capacity. Sorting or ordered lookups guarantee that each match uses the smallest feasible trainer.

Recommended for interviews: The sorted two-pointer approach is the expected solution. It is short, easy to reason about, and clearly demonstrates greedy thinking combined with efficient iteration over arrays. Interviewers typically expect O(n log n) due to sorting, followed by a linear scan. The ordered set method shows deeper knowledge of data structures but is rarely necessary unless the problem introduces dynamic updates.

Approach 1: Two-Pointer Technique after Sorting

This approach involves sorting both the players and trainers arrays. Then, using a two-pointer method, we try to find the maximum number of pairings possible. We move pointers on players and trainers based on the comparison between the player's ability and the trainer's capacity.

The solution starts by sorting both arrays, players and trainers. Then, it utilizes two pointers, one for each array. If the current player's ability is less than or equal to the current trainer's capacity, a match is counted, and both pointers advance. If not, only the trainer's pointer advances to find a suitable trainer.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n log n + m log m), where n and m are the lengths of the players and trainers arrays respectively, due to sorting.
Space Complexity: O(1), as no additional space proportional to input size is used.

Try this approach in the editor →

Approach 2: Greedy Matching with Sets

This approach is based on using a greedy algorithm with the help of sets or heaps to facilitate a similar matching process. By leveraging the properties of sets, we can efficiently manage player-trainer pairings as we iterate through sorted lists.

In this C++ solution, we use a multiset to store trainers. We sort players and use the lower_bound function to find the minimum capacity trainer for each player efficiently. Once a match is found, the trainer is removed from the set.

Code

C++

Python

Complexity

Time Complexity: O(n log n + m log m) for sorting and finding matches.
Space Complexity: O(m) for storing trainers.

Try this approach in the editor →

Approach 3: Greedy + Two Pointers

According to the problem description, each athlete should be matched with the trainer whose ability value is as close as possible. Therefore, we can sort the ability values of both athletes and trainers, and then use the two-pointer method for matching.

We use two pointers i and j to point to the arrays of athletes and trainers, respectively, both initially pointing to the start of the arrays. Then we traverse the ability values of the athletes one by one. If the current trainer's ability value is less than the current athlete's ability value, we move the trainer's pointer to the right by one position until we find a trainer whose ability value is greater than or equal to the current athlete's. If no such trainer is found, it means the current athlete cannot be matched with any trainer, and we return the current athlete's index. Otherwise, we can match the current athlete with the trainer, and then move both pointers to the right by one position. Continue this process until all athletes have been traversed.

If we traverse all athletes, it means all athletes can be matched with trainers, and we return the number of athletes.

The time complexity is O(m times log m + n times log n), and the space complexity is O(max(log m, log n)). Here, m and n are the numbers of athletes and trainers, respectively.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Two-Pointer Technique after Sorting

Time Complexity: O(n log n + m log m), where n and m are the lengths of the players and trainers arrays respectively, due to sorting.
Space Complexity: O(1), as no additional space proportional to input size is used.

Greedy Matching with Sets

Time Complexity: O(n log n + m log m) for sorting and finding matches.
Space Complexity: O(m) for storing trainers.

Greedy + Two Pointers—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Two-Pointer Technique after SortingO(n log n)O(1)Best general solution; simple greedy matching after sorting both arrays
Greedy Matching with Ordered SetO(n log n)O(n)Useful when trainers must be searched dynamically using lower_bound lookups

Video Solution

Maximum Matching of Players With Trainers | Simple Intuition | Leetcode 2410 | codestorywithMIK • codestorywithMIK • 4,154 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Maximum Matching of Players With Trainers easy or hard?
The problem is rated Medium because the greedy idea must be recognized. Once you realize that weaker players should use the smallest valid trainers, the implementation becomes straightforward with sorting and two pointers.
Maximum Matching of Players With Trainers Python/Java solution
In Python or Java, sort both arrays and iterate with two indices. When trainer >= player, increment the match count and move both pointers; otherwise move the trainer pointer. This greedy scan produces the maximum number of valid matches in O(n log n) time.
How to solve Maximum Matching of Players With Trainers in O(n)?
True O(n) time is generally not achievable because the arrays are unsorted and require ordering to guarantee optimal greedy matching. The best practical solution is O(n log n) using sorting followed by a linear two-pointer pass.
What is the best approach for Maximum Matching of Players With Trainers?
The optimal solution sorts both players and trainers, then uses a greedy two-pointer scan. Always try to match the smallest player with the smallest trainer capable of training them. This avoids wasting stronger trainers early. The complexity is O(n log n) due to sorting and O(1) extra space.
Is Maximum Matching of Players With Trainers asked at Google/Amazon/Meta?
Greedy matching and two-pointer problems like this appear frequently in interviews at companies such as Amazon, Google, and Meta. Variants involving pairing resources with constraints or scheduling tasks are common interview patterns.
What data structure is used in Maximum Matching of Players With Trainers?
The typical solution uses arrays combined with sorting and two pointers. An alternative solution uses ordered data structures such as a multiset or balanced binary search tree to perform lower_bound lookups for the smallest valid trainer.
What is the time complexity of Maximum Matching of Players With Trainers?
The optimal algorithm runs in O(n log n) time because both arrays must be sorted first. After sorting, a single linear scan with two pointers matches players and trainers in O(n). Space complexity can be O(1) if sorting is done in-place.

Ready to solve this problem?

Practice Maximum Matching of Players With Trainers with our built-in code editor and test cases.

Practice on FleetCode