less than 1 minute read

Problem Statement

leetcode problem link

Brute Force [Accepted]

class Solution:
    def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
        res = 0
        player_heap = []
        for player in players:
            heapq.heappush(player_heap, -player)
        trainer_heap = []
        for trainer in trainers:
            heapq.heappush(trainer_heap, -trainer)

        while trainer_heap:
            trainer = -heapq.heappop(trainer_heap)
            while player_heap and -player_heap[0] > trainer:
                heapq.heappop(player_heap)
            if not player_heap:
                break
            player = -heapq.heappop(player_heap)
            if player <= trainer:
                res += 1
        return res

Editorial

Approach: Sorting + Two Pointers + Greedy

class Solution:
    def matchPlayersAndTrainers(
        self, players: List[int], trainers: List[int]
    ) -> int:
        players.sort()
        trainers.sort()
        m, n = len(players), len(trainers)
        i = j = count = 0

        while i < m and j < n:
            while j < n and players[i] > trainers[j]:
                j += 1
            if j < n:
                count += 1
            i += 1
            j += 1

        return count