Skip to content

Commit 0159755

Browse files
authored
feat: add solutions to lc problem: No.2410 (#3523)
No.2410.Maximum Matching of Players With Trainers
1 parent 6724a40 commit 0159755

File tree

7 files changed

+148
-100
lines changed

7 files changed

+148
-100
lines changed

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

+52-33
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,13 @@ tags:
6666

6767
### 方法一:贪心 + 双指针
6868

69-
按运动员的能力值从小到大排序,选择大于等于运动员能力值的,且自身能力值最小的训练师
69+
根据题目描述,每位运动员应该尽可能匹配能力值最接近的训练师,因此我们可以对运动员和训练师的能力值进行排序,然后使用双指针的方法进行匹配
7070

71-
时间复杂度 $O(n \times \log n + m \times \log m)$,空间复杂度 $O(\log n + \log m)$。其中 $n$ 和 $m$ 分别为运动员和训练师的数量。
71+
我们用两个指针 $i$ 和 $j$ 分别指向运动员和训练师的数组,初始时都指向数组的起始位置。然后我们逐个遍历运动员的能力值,如果当前训练师的能力值小于当前运动员的能力值,我们就将训练师的指针向右移动一位,直到找到一个能力值大于等于当前运动员的训练师。如果找不到这样的训练师,说明当前运动员无法匹配任何训练师,此时我们返回当前运动员的下标即可。否则,,我们可以将当前运动员和训练师匹配,然后将两个指针都向右移动一位。继续这个过程直到遍历完所有的运动员。
72+
73+
如果我们遍历完所有的运动员,说明所有的运动员都能匹配到训练师,此时我们返回运动员的数量即可。
74+
75+
时间复杂度 $O(m \times \log m + n \times \log n)$,空间复杂度 $O(\max(\log m, \log n))$。其中 $m$ 和 $n$ 分别为运动员和训练师的数量。
7276

7377
<!-- tabs:start -->
7478

@@ -79,14 +83,14 @@ class Solution:
7983
def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
8084
players.sort()
8185
trainers.sort()
82-
ans = j = 0
83-
for p in players:
84-
while j < len(trainers) and trainers[j] < p:
86+
j, n = 0, len(trainers)
87+
for i, p in enumerate(players):
88+
while j < n and trainers[j] < p:
8589
j += 1
86-
if j < len(trainers):
87-
ans += 1
88-
j += 1
89-
return ans
90+
if j == n:
91+
return i
92+
j += 1
93+
return len(players)
9094
```
9195

9296
#### Java
@@ -96,18 +100,16 @@ class Solution {
96100
public int matchPlayersAndTrainers(int[] players, int[] trainers) {
97101
Arrays.sort(players);
98102
Arrays.sort(trainers);
99-
int ans = 0;
100-
int j = 0;
101-
for (int p : players) {
102-
while (j < trainers.length && trainers[j] < p) {
103+
int m = players.length, n = trainers.length;
104+
for (int i = 0, j = 0; i < m; ++i, ++j) {
105+
while (j < n && trainers[j] < players[i]) {
103106
++j;
104107
}
105-
if (j < trainers.length) {
106-
++ans;
107-
++j;
108+
if (j == n) {
109+
return i;
108110
}
109111
}
110-
return ans;
112+
return m;
111113
}
112114
}
113115
```
@@ -118,19 +120,18 @@ class Solution {
118120
class Solution {
119121
public:
120122
int matchPlayersAndTrainers(vector<int>& players, vector<int>& trainers) {
121-
sort(players.begin(), players.end());
122-
sort(trainers.begin(), trainers.end());
123-
int ans = 0, j = 0;
124-
for (int p : players) {
125-
while (j < trainers.size() && trainers[j] < p) {
123+
ranges::sort(players);
124+
ranges::sort(trainers);
125+
int m = players.size(), n = trainers.size();
126+
for (int i = 0, j = 0; i < m; ++i, ++j) {
127+
while (j < n && trainers[j] < players[i]) {
126128
++j;
127129
}
128-
if (j < trainers.size()) {
129-
++ans;
130-
++j;
130+
if (j == n) {
131+
return i;
131132
}
132133
}
133-
return ans;
134+
return m;
134135
}
135136
};
136137
```
@@ -141,17 +142,35 @@ public:
141142
func matchPlayersAndTrainers(players []int, trainers []int) int {
142143
sort.Ints(players)
143144
sort.Ints(trainers)
144-
ans, j := 0, 0
145-
for _, p := range players {
146-
for j < len(trainers) && trainers[j] < p {
145+
m, n := len(players), len(trainers)
146+
for i, j := 0, 0; i < m; i, j = i+1, j+1 {
147+
for j < n && trainers[j] < players[i] {
147148
j++
148149
}
149-
if j < len(trainers) {
150-
ans++
151-
j++
150+
if j == n {
151+
return i
152152
}
153153
}
154-
return ans
154+
return m
155+
}
156+
```
157+
158+
#### TypeScript
159+
160+
```ts
161+
function matchPlayersAndTrainers(players: number[], trainers: number[]): number {
162+
players.sort((a, b) => a - b);
163+
trainers.sort((a, b) => a - b);
164+
const [m, n] = [players.length, trainers.length];
165+
for (let i = 0, j = 0; i < m; ++i, ++j) {
166+
while (j < n && trainers[j] < players[i]) {
167+
++j;
168+
}
169+
if (j === n) {
170+
return i;
171+
}
172+
}
173+
return m;
155174
}
156175
```
157176

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

+52-33
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,13 @@ Each player can only be matched with one trainer, so the maximum answer is 1.
6969

7070
### Solution 1: Greedy + Two Pointers
7171

72-
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.
72+
According to the problem description, each athlete should be matched with the trainer whose ability value is as close as possible. Therefore, we can sort the ability values of both athletes and trainers, and then use the two-pointer method for matching.
7373

74-
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.
74+
We use two pointers $i$ and $j$ to point to the arrays of athletes and trainers, respectively, both initially pointing to the start of the arrays. Then we traverse the ability values of the athletes one by one. If the current trainer's ability value is less than the current athlete's ability value, we move the trainer's pointer to the right by one position until we find a trainer whose ability value is greater than or equal to the current athlete's. If no such trainer is found, it means the current athlete cannot be matched with any trainer, and we return the current athlete's index. Otherwise, we can match the current athlete with the trainer, and then move both pointers to the right by one position. Continue this process until all athletes have been traversed.
75+
76+
If we traverse all athletes, it means all athletes can be matched with trainers, and we return the number of athletes.
77+
78+
The time complexity is $O(m \times \log m + n \times \log n)$, and the space complexity is $O(\max(\log m, \log n))$. Here, $m$ and $n$ are the numbers of athletes and trainers, respectively.
7579

7680
<!-- tabs:start -->
7781

@@ -82,14 +86,14 @@ class Solution:
8286
def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
8387
players.sort()
8488
trainers.sort()
85-
ans = j = 0
86-
for p in players:
87-
while j < len(trainers) and trainers[j] < p:
89+
j, n = 0, len(trainers)
90+
for i, p in enumerate(players):
91+
while j < n and trainers[j] < p:
8892
j += 1
89-
if j < len(trainers):
90-
ans += 1
91-
j += 1
92-
return ans
93+
if j == n:
94+
return i
95+
j += 1
96+
return len(players)
9397
```
9498

9599
#### Java
@@ -99,18 +103,16 @@ class Solution {
99103
public int matchPlayersAndTrainers(int[] players, int[] trainers) {
100104
Arrays.sort(players);
101105
Arrays.sort(trainers);
102-
int ans = 0;
103-
int j = 0;
104-
for (int p : players) {
105-
while (j < trainers.length && trainers[j] < p) {
106+
int m = players.length, n = trainers.length;
107+
for (int i = 0, j = 0; i < m; ++i, ++j) {
108+
while (j < n && trainers[j] < players[i]) {
106109
++j;
107110
}
108-
if (j < trainers.length) {
109-
++ans;
110-
++j;
111+
if (j == n) {
112+
return i;
111113
}
112114
}
113-
return ans;
115+
return m;
114116
}
115117
}
116118
```
@@ -121,19 +123,18 @@ class Solution {
121123
class Solution {
122124
public:
123125
int matchPlayersAndTrainers(vector<int>& players, vector<int>& trainers) {
124-
sort(players.begin(), players.end());
125-
sort(trainers.begin(), trainers.end());
126-
int ans = 0, j = 0;
127-
for (int p : players) {
128-
while (j < trainers.size() && trainers[j] < p) {
126+
ranges::sort(players);
127+
ranges::sort(trainers);
128+
int m = players.size(), n = trainers.size();
129+
for (int i = 0, j = 0; i < m; ++i, ++j) {
130+
while (j < n && trainers[j] < players[i]) {
129131
++j;
130132
}
131-
if (j < trainers.size()) {
132-
++ans;
133-
++j;
133+
if (j == n) {
134+
return i;
134135
}
135136
}
136-
return ans;
137+
return m;
137138
}
138139
};
139140
```
@@ -144,17 +145,35 @@ public:
144145
func matchPlayersAndTrainers(players []int, trainers []int) int {
145146
sort.Ints(players)
146147
sort.Ints(trainers)
147-
ans, j := 0, 0
148-
for _, p := range players {
149-
for j < len(trainers) && trainers[j] < p {
148+
m, n := len(players), len(trainers)
149+
for i, j := 0, 0; i < m; i, j = i+1, j+1 {
150+
for j < n && trainers[j] < players[i] {
150151
j++
151152
}
152-
if j < len(trainers) {
153-
ans++
154-
j++
153+
if j == n {
154+
return i
155155
}
156156
}
157-
return ans
157+
return m
158+
}
159+
```
160+
161+
#### TypeScript
162+
163+
```ts
164+
function matchPlayersAndTrainers(players: number[], trainers: number[]): number {
165+
players.sort((a, b) => a - b);
166+
trainers.sort((a, b) => a - b);
167+
const [m, n] = [players.length, trainers.length];
168+
for (let i = 0, j = 0; i < m; ++i, ++j) {
169+
while (j < n && trainers[j] < players[i]) {
170+
++j;
171+
}
172+
if (j === n) {
173+
return i;
174+
}
175+
}
176+
return m;
158177
}
159178
```
160179

Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
class Solution {
22
public:
33
int matchPlayersAndTrainers(vector<int>& players, vector<int>& trainers) {
4-
sort(players.begin(), players.end());
5-
sort(trainers.begin(), trainers.end());
6-
int ans = 0, j = 0;
7-
for (int p : players) {
8-
while (j < trainers.size() && trainers[j] < p) {
4+
ranges::sort(players);
5+
ranges::sort(trainers);
6+
int m = players.size(), n = trainers.size();
7+
for (int i = 0, j = 0; i < m; ++i, ++j) {
8+
while (j < n && trainers[j] < players[i]) {
99
++j;
1010
}
11-
if (j < trainers.size()) {
12-
++ans;
13-
++j;
11+
if (j == n) {
12+
return i;
1413
}
1514
}
16-
return ans;
15+
return m;
1716
}
18-
};
17+
};
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
func matchPlayersAndTrainers(players []int, trainers []int) int {
22
sort.Ints(players)
33
sort.Ints(trainers)
4-
ans, j := 0, 0
5-
for _, p := range players {
6-
for j < len(trainers) && trainers[j] < p {
4+
m, n := len(players), len(trainers)
5+
for i, j := 0, 0; i < m; i, j = i+1, j+1 {
6+
for j < n && trainers[j] < players[i] {
77
j++
88
}
9-
if j < len(trainers) {
10-
ans++
11-
j++
9+
if j == n {
10+
return i
1211
}
1312
}
14-
return ans
15-
}
13+
return m
14+
}

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

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ class Solution {
22
public int matchPlayersAndTrainers(int[] players, int[] trainers) {
33
Arrays.sort(players);
44
Arrays.sort(trainers);
5-
int ans = 0;
6-
int j = 0;
7-
for (int p : players) {
8-
while (j < trainers.length && trainers[j] < p) {
5+
int m = players.length, n = trainers.length;
6+
for (int i = 0, j = 0; i < m; ++i, ++j) {
7+
while (j < n && trainers[j] < players[i]) {
98
++j;
109
}
11-
if (j < trainers.length) {
12-
++ans;
13-
++j;
10+
if (j == n) {
11+
return i;
1412
}
1513
}
16-
return ans;
14+
return m;
1715
}
18-
}
16+
}

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ class Solution:
22
def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
33
players.sort()
44
trainers.sort()
5-
ans = j = 0
6-
for p in players:
7-
while j < len(trainers) and trainers[j] < p:
5+
j, n = 0, len(trainers)
6+
for i, p in enumerate(players):
7+
while j < n and trainers[j] < p:
88
j += 1
9-
if j < len(trainers):
10-
ans += 1
11-
j += 1
12-
return ans
9+
if j == n:
10+
return i
11+
j += 1
12+
return len(players)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function matchPlayersAndTrainers(players: number[], trainers: number[]): number {
2+
players.sort((a, b) => a - b);
3+
trainers.sort((a, b) => a - b);
4+
const [m, n] = [players.length, trainers.length];
5+
for (let i = 0, j = 0; i < m; ++i, ++j) {
6+
while (j < n && trainers[j] < players[i]) {
7+
++j;
8+
}
9+
if (j === n) {
10+
return i;
11+
}
12+
}
13+
return m;
14+
}

0 commit comments

Comments
 (0)