Skip to content

2410. Maximum Matching of Players With Trainers 👍

  • Time: $O(\texttt{sort})$
  • Space: $O(\texttt{sort})$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
 public:
  int matchPlayersAndTrainers(vector<int>& players, vector<int>& trainers) {
    int ans = 0;

    ranges::sort(players);
    ranges::sort(trainers);

    for (int i = 0; i < trainers.size(); ++i)
      if (players[ans] <= trainers[i] && ++ans == players.size())
        return ans;

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
  public int matchPlayersAndTrainers(int[] players, int[] trainers) {
    int ans = 0;

    Arrays.sort(players);
    Arrays.sort(trainers);

    for (int i = 0; i < trainers.length; ++i)
      if (players[ans] <= trainers[i] && ++ans == players.length)
        return ans;

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution:
  def matchPlayersAndTrainers(
      self,
      players: list[int],
      trainers: list[int],
  ) -> int:
    ans = 0

    players.sort()
    trainers.sort()

    for i, trainer in enumerate(trainers):
      if players[ans] <= trainer:
        ans += 1
        if ans == len(players):
          return ans

    return ans