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.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We want to find the largest number of player-trainer pairs where the player's skill is not greater than the trainer's ability. The brute force approach is to consider every possible combination of players and trainers to see if they can be matched.
Here's how the algorithm would work step-by-step:
def maximum_matching_brute_force(players, trainers):
maximum_matches = 0
def find_maximum_matches(current_player_index, used_trainers, current_matches):
nonlocal maximum_matches
# If we've considered all players, update the maximum matches
if current_player_index == len(players):
maximum_matches = max(maximum_matches, current_matches)
return
# Try matching the current player with each available trainer
for trainer_index in range(len(trainers)):
if trainer_index not in used_trainers:
# Check if the player's skill is less than or equal to the trainer's ability
if players[current_player_index] <= trainers[trainer_index]:
# If it's a valid match, recursively explore further matches
find_maximum_matches(
current_player_index + 1,
used_trainers | {trainer_index},
current_matches + 1,
)
# If the current player cannot be matched with any trainer,
# move on to the next player without increasing the match count
find_maximum_matches(
current_player_index + 1,
used_trainers,
current_matches,
)
find_maximum_matches(0, set(), 0)
#Return the maximum number of matches found
return maximum_matchesThe best way to match players and trainers efficiently is to first organize them by skill level. Then, we'll go through the players one by one, finding the smallest suitable trainer for each, ensuring we don't reuse trainers.
Here's how the algorithm would work step-by-step:
def maximum_matching(players, trainers):
players.sort()
trainers.sort()
matches_count = 0
trainer_index = 0
# Iterate through each player to find a suitable trainer
for player_skill in players:
# Find the first trainer that meets the player's skill req.
while trainer_index < len(trainers) and \
trainers[trainer_index] < player_skill:
trainer_index += 1
# If a suitable trainer is found, increment matches
if trainer_index < len(trainers):
matches_count += 1
trainer_index += 1
return matches_count| Case | How to Handle |
|---|---|
| Null or empty players array | Return 0 if either the players or trainers array is null or empty, indicating no matchings possible. |
| Null or empty trainers array | Return 0 if either the players or trainers array is null or empty, indicating no matchings possible. |
| Players array with a single element | If the trainers array contains an element greater than or equal to the player, return 1, otherwise 0. |
| Trainers array with a single element | If the players array contains an element less than or equal to the trainer, return 1, otherwise 0. |
| Players and Trainers arrays contain identical values | The greedy matching approach will correctly identify the maximum number of matches. |
| Players array with very large sizes, trainers array with smaller sizes, and no matching possible | The sorting-based approach still functions, but the runtime is dominated by sorting and iterating to the end results in 0. |
| Integer overflow when comparing extreme values | Using a language with arbitrary-precision integers or explicitly check for potential overflows before comparison |
| Both arrays are very large | Ensure the sorting algorithm used (if any) has acceptable time complexity (e.g., O(n log n) or O(m log m)) to avoid timeouts. |