---
# System prepended metadata

title: 2410. Maximum Matching of Players With Trainers
tags: [Leetcode, Two Pointers, Sorting, Medium, Greedy]

---

# 2410. Maximum Matching of Players With Trainers
###### tags: `Leetcode` `Medium` `Sorting` `Greedy` `Two Pointers`
## Code
### Two Pointers
```java=
class Solution {
    public int matchPlayersAndTrainers(int[] players, int[] trainers) {
        int ans = 0;
        Arrays.sort(players);
        Arrays.sort(trainers);
        int p1 = 0, p2 = 0;
        for(p1=0; p1<players.length; p1++){
            while(p2<trainers.length && players[p1]>trainers[p2]){
                p2++;
            }
            if(p2==trainers.length) break;
            else{
                p2++;
                ans++;
            }
        }
        return ans;
    }
}
```
### TreeMap
```java=
class Solution {
    public int matchPlayersAndTrainers(int[] players, int[] trainers) {
        TreeMap<Integer, Integer> trainerCnt = new TreeMap<>();
        for(int t:trainers){
            trainerCnt.put(t, trainerCnt.getOrDefault(t,0)+1);
        }
        int ans = 0;
        for(int p:players){
            Integer trainer = trainerCnt.ceilingKey(p);
            if(trainer==null) continue;
            else{
                ans++;
                trainerCnt.put(trainer, trainerCnt.get(trainer)-1);
                if(trainerCnt.get(trainer)==0) trainerCnt.remove(trainer);
            }
        }
        return ans;
    }
}
```