Skip to content

feat: add solutions to lc problem: No.2410 #3523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ tags:

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

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

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

如果我们遍历完所有的运动员,说明所有的运动员都能匹配到训练师,此时我们返回运动员的数量即可。

时间复杂度 $O(m \times \log m + n \times \log n)$,空间复杂度 $O(\max(\log m, \log n))$。其中 $m$ 和 $n$ 分别为运动员和训练师的数量。

<!-- tabs:start -->

Expand All @@ -79,14 +83,14 @@ 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, n = 0, len(trainers)
for i, p in enumerate(players):
while j < n and trainers[j] < p:
j += 1
if j < len(trainers):
ans += 1
j += 1
return ans
if j == n:
return i
j += 1
return len(players)
```

#### Java
Expand All @@ -96,18 +100,16 @@ 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) {
int m = players.length, n = trainers.length;
for (int i = 0, j = 0; i < m; ++i, ++j) {
while (j < n && trainers[j] < players[i]) {
++j;
}
if (j < trainers.length) {
++ans;
++j;
if (j == n) {
return i;
}
}
return ans;
return m;
}
}
```
Expand All @@ -118,19 +120,18 @@ class Solution {
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) {
ranges::sort(players);
ranges::sort(trainers);
int m = players.size(), n = trainers.size();
for (int i = 0, j = 0; i < m; ++i, ++j) {
while (j < n && trainers[j] < players[i]) {
++j;
}
if (j < trainers.size()) {
++ans;
++j;
if (j == n) {
return i;
}
}
return ans;
return m;
}
};
```
Expand All @@ -141,17 +142,35 @@ public:
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 {
m, n := len(players), len(trainers)
for i, j := 0, 0; i < m; i, j = i+1, j+1 {
for j < n && trainers[j] < players[i] {
j++
}
if j < len(trainers) {
ans++
j++
if j == n {
return i
}
}
return ans
return m
}
```

#### TypeScript

```ts
function matchPlayersAndTrainers(players: number[], trainers: number[]): number {
players.sort((a, b) => a - b);
trainers.sort((a, b) => a - b);
const [m, n] = [players.length, trainers.length];
for (let i = 0, j = 0; i < m; ++i, ++j) {
while (j < n && trainers[j] < players[i]) {
++j;
}
if (j === n) {
return i;
}
}
return m;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ Each player can only be matched with one trainer, so the maximum answer is 1.

### 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.
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.

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.
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.

If we traverse all athletes, it means all athletes can be matched with trainers, and we return the number of athletes.

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.

<!-- tabs:start -->

Expand All @@ -82,14 +86,14 @@ 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, n = 0, len(trainers)
for i, p in enumerate(players):
while j < n and trainers[j] < p:
j += 1
if j < len(trainers):
ans += 1
j += 1
return ans
if j == n:
return i
j += 1
return len(players)
```

#### Java
Expand All @@ -99,18 +103,16 @@ 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) {
int m = players.length, n = trainers.length;
for (int i = 0, j = 0; i < m; ++i, ++j) {
while (j < n && trainers[j] < players[i]) {
++j;
}
if (j < trainers.length) {
++ans;
++j;
if (j == n) {
return i;
}
}
return ans;
return m;
}
}
```
Expand All @@ -121,19 +123,18 @@ class Solution {
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) {
ranges::sort(players);
ranges::sort(trainers);
int m = players.size(), n = trainers.size();
for (int i = 0, j = 0; i < m; ++i, ++j) {
while (j < n && trainers[j] < players[i]) {
++j;
}
if (j < trainers.size()) {
++ans;
++j;
if (j == n) {
return i;
}
}
return ans;
return m;
}
};
```
Expand All @@ -144,17 +145,35 @@ public:
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 {
m, n := len(players), len(trainers)
for i, j := 0, 0; i < m; i, j = i+1, j+1 {
for j < n && trainers[j] < players[i] {
j++
}
if j < len(trainers) {
ans++
j++
if j == n {
return i
}
}
return ans
return m
}
```

#### TypeScript

```ts
function matchPlayersAndTrainers(players: number[], trainers: number[]): number {
players.sort((a, b) => a - b);
trainers.sort((a, b) => a - b);
const [m, n] = [players.length, trainers.length];
for (let i = 0, j = 0; i < m; ++i, ++j) {
while (j < n && trainers[j] < players[i]) {
++j;
}
if (j === n) {
return i;
}
}
return m;
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
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) {
ranges::sort(players);
ranges::sort(trainers);
int m = players.size(), n = trainers.size();
for (int i = 0, j = 0; i < m; ++i, ++j) {
while (j < n && trainers[j] < players[i]) {
++j;
}
if (j < trainers.size()) {
++ans;
++j;
if (j == n) {
return i;
}
}
return ans;
return m;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
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 {
m, n := len(players), len(trainers)
for i, j := 0, 0; i < m; i, j = i+1, j+1 {
for j < n && trainers[j] < players[i] {
j++
}
if j < len(trainers) {
ans++
j++
if j == n {
return i
}
}
return ans
}
return m
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ 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) {
int m = players.length, n = trainers.length;
for (int i = 0, j = 0; i < m; ++i, ++j) {
while (j < n && trainers[j] < players[i]) {
++j;
}
if (j < trainers.length) {
++ans;
++j;
if (j == n) {
return i;
}
}
return ans;
return m;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ 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, n = 0, len(trainers)
for i, p in enumerate(players):
while j < n and trainers[j] < p:
j += 1
if j < len(trainers):
ans += 1
j += 1
return ans
if j == n:
return i
j += 1
return len(players)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function matchPlayersAndTrainers(players: number[], trainers: number[]): number {
players.sort((a, b) => a - b);
trainers.sort((a, b) => a - b);
const [m, n] = [players.length, trainers.length];
for (let i = 0, j = 0; i < m; ++i, ++j) {
while (j < n && trainers[j] < players[i]) {
++j;
}
if (j === n) {
return i;
}
}
return m;
}
Loading