返回介绍

solution / 2400-2499 / 2410.Maximum Matching of Players With Trainers / README_EN

发布于 2024-06-17 01:03:06 字数 4577 浏览 0 评论 0 收藏 0

2410. Maximum Matching of Players With Trainers

中文文档

Description

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

Solutions

Solution 1: Greedy + Two Pointers

Sort the athletes by their abilities in ascending order, and select the trainer with the smallest ability that is greater than or equal to the athlete's ability.

The time complexity is $O(n \times \log n + m \times \log m)$, and the space complexity is $O(\log n + \log m)$. Here, $n$ and $m$ are the number of athletes and trainers, respectively.

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
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 {
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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文