Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2410.html

2410. Maximum Matching of Players With Trainers

  • Difficulty: Medium.
  • Related Topics: .
  • Similar Questions: Most Profit Assigning Work, Long Pressed Name, Interval List Intersections, Largest Merge Of Two Strings, Maximum Number of Tasks You Can Assign, Successful Pairs of Spells and Potions, The Latest Time to Catch a Bus.

Problem

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

Solution (Java, C++, Python)

  • class Solution {
        public int matchPlayersAndTrainers(int[] player, int[] train) {
            Arrays.sort(player);
            Arrays.sort(train);
            int i = 0, j = 0, res = 0;
            while(j<train.length && i<player.length) {
                if(player[i]>train[j])
                    while(j<train.length && train[j]<player[i]) // find trainer
                        j++;
                else { res++; i++; j++; } // can train.
            }
            return res;
        }
    }
    
    ############
    
    class Solution {
        public int matchPlayersAndTrainers(int[] players, int[] trainers) {
            Arrays.sort(players);
            Arrays.sort(trainers);
            int ans = 0;
            int j = 0;
            for (int p : players) {
                while (j < trainers.length && trainers[j] < p) {
                    ++j;
                }
                if (j < trainers.length) {
                    ++ans;
                    ++j;
                }
            }
            return ans;
        }
    }
    
  • class Solution:
        def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
            players.sort()
            trainers.sort()
            ans = j = 0
            for p in players:
                while j < len(trainers) and trainers[j] < p:
                    j += 1
                if j < len(trainers):
                    ans += 1
                    j += 1
            return ans
    
    ############
    
    # 2410. Maximum Matching of Players With Trainers
    # https://leetcode.com/problems/maximum-matching-of-players-with-trainers/
    
    class Solution:
        def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
            N, M = len(players), len(trainers)
            players.sort()
            trainers.sort()
            res = 0
            firstPlayer = players[0]
            j = bisect_left(trainers, firstPlayer)
            
            if j == M: return 0
            
            for player in players:
                while j < M and player > trainers[j]:
                    j += 1
                    
                if j == M: break
                    
                if player <= trainers[j]:
                    res += 1
                    j += 1
    
            return res
    
    
  • class Solution {
    public:
        int matchPlayersAndTrainers(vector<int>& players, vector<int>& trainers) {
            sort(players.begin(), players.end());
            sort(trainers.begin(), trainers.end());
            int ans = 0, j = 0;
            for (int p : players) {
                while (j < trainers.size() && trainers[j] < p) {
                    ++j;
                }
                if (j < trainers.size()) {
                    ++ans;
                    ++j;
                }
            }
            return ans;
        }
    };
    
  • func matchPlayersAndTrainers(players []int, trainers []int) int {
    	sort.Ints(players)
    	sort.Ints(trainers)
    	ans, j := 0, 0
    	for _, p := range players {
    		for j < len(trainers) && trainers[j] < p {
    			j++
    		}
    		if j < len(trainers) {
    			ans++
    			j++
    		}
    	}
    	return ans
    }
    

Explain:

nope.

Complexity:

  • Time complexity : O(n).
  • Space complexity : O(n).

All Problems

All Solutions