↓Skip to main content
  1. LeetCode/

LeetCode 2410: Maximum Matching of Players With Trainers

·2 mins· ·
LeetCode Medium Two-Pointers Greedy Sorting Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-07-13

  • Submissions: Accepted (first try)

Approach
#

Greedy: the weakest player should take the smallest trainer that is just enough. Saving larger trainers for stronger players never reduces the number of matches.

Sort players and trainers and move two pointers in the same direction:

  • trainers[j] < players[i]: this trainer cannot handle even the weakest remaining player, and later players are stronger, so skip the trainer.
  • Otherwise match them and advance both pointers.

The number of matched players is the answer.

My Solution
#

class Solution:
    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
        players.sort()
        trainers.sort()
        i = j = 0
        while i < len(players) and j < len(trainers):
            if trainers[j] >= players[i]:
                i += 1                     # matched
            j += 1                         # trainer used or skipped
        return i

Complexity
#

Time O(n log n + m log m) for sorting, space O(1) apart from sorting.

Mistakes to Avoid
#

  • Matching a player with the largest trainer: it wastes a trainer that could serve a stronger player.

Interview Explanation
#

I sort both arrays and give the weakest player the smallest trainer that is strong enough. A trainer that is too weak is skipped, since later players are only stronger; otherwise they match and both pointers advance. Each element is visited once, so sorting dominates at O(n log n).

Related

LeetCode 3075: Maximize Happiness of Selected Children
·1 min
LeetCode Medium Array Greedy Sorting
LeetCode 3075 note: choose k children from the happiest, where the i-th choice loses i, floored at 0
LeetCode 611: Valid Triangle Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Greedy
LeetCode 611 note: sort, fix the longest side k, and use two pointers for the shorter sides; when the sum is large enough every i in between works, adding j - i at once
LeetCode 324: Wiggle Sort II
·2 mins
LeetCode Medium Array Divide-and-Conquer Greedy Sorting
LeetCode 324 note: sort, split into a small half and a large half, reverse each, and interleave with small values at even indices and large ones at odd indices
LeetCode 435: Non-overlapping Intervals
·2 mins
LeetCode Medium Array Dynamic-Programming Greedy Sorting
LeetCode 435 note: minimum removals = total minus the maximum kept; sort by right end and greedily keep the one ending earliest
LeetCode 75: Sort Colors
·2 mins
LeetCode Medium Array Two-Pointers Sorting Quicksort
LeetCode 75 note: the Dutch flag, with 0s left of low and 2s right of high; after swapping a 2 to high, do not advance, since the swapped-in value is unchecked
LeetCode 11: Container With Most Water
·2 mins
LeetCode Medium Array Two-Pointers Greedy
LeetCode 11 note: start pointers at both ends and always move the shorter side; moving the taller side only narrows the width without raising the height