Maximum matching of players with trainers

2021 年 6 月 11 日 星期五(已编辑)
/ ,
6
摘要
The problem involves matching players with trainers by sorting two arrays and using a two-pointer technique. The solution has a time complexity of O(n log n) due to sorting and a space complexity of O(log n). It was completed in 7 minutes and 41 seconds.
这篇文章上次修改于 2025 年 7 月 12 日 星期六,可能部分内容已经不适用,如有疑问可询问作者。

Maximum matching of players with trainers

Question

Analysis

  1. Sort two arrays
  2. Two pointer to record matching position

Timing

7 min 41 sec

Code

class Solution {
    public int matchPlayersAndTrainers(int[] players, int[] trainers) {
        // 2 5 8 8
        //   4 7 9
        Arrays.sort(players);
        Arrays.sort(trainers);

        int m = 0;
        int n = 0;
        int res = 0;
        while (m < players.length && n < trainers.length) {
            if (players[m] <= trainers[n]) {
                res++;
                m++;
            } 
            n++;
        }
        return res;
    }
}

Completixy

Time O(nlog(n)) from sorting

Space O(log(n)) from sorting

使用社交账号登录

  • Loading...
  • Loading...
  • Loading...
  • Loading...
  • Loading...