Skip to content

Commit f040fb4

Browse files
committed
Add solution and test-cases for problem 2410
1 parent 8ae5d85 commit f040fb4

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

leetcode/2401-2500/2410.Maximum-Matching-of-Players-With-Trainers/README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
# [2410.Maximum Matching of Players With Trainers][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
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.
7+
8+
Return the **maximum** number of matchings between `players` and `trainers` that satisfy these conditions.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: players = [4,7,9], trainers = [8,2,5,8]
14+
Output: 2
15+
Explanation:
16+
One of the ways we can form two matchings is as follows:
17+
- players[0] can be matched with trainers[0] since 4 <= 8.
18+
- players[1] can be matched with trainers[3] since 7 <= 8.
19+
It can be proven that 2 is the maximum number of matchings that can be formed.
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
1723

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Maximum Matching of Players With Trainers
23-
```go
2424
```
25-
25+
Input: players = [1,1,1], trainers = [10]
26+
Output: 1
27+
Explanation:
28+
The trainer can be matched with any of the 3 players.
29+
Each player can only be matched with one trainer, so the maximum answer is 1.
30+
```
2631

2732
## 结语
2833

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(players []int, trainers []int) int {
6+
sort.Ints(players)
7+
sort.Ints(trainers)
8+
i, j := 0, 0
9+
ans := 0
10+
for i < len(players) && j < len(trainers) {
11+
if trainers[j] >= players[i] {
12+
i, j = i+1, j+1
13+
ans++
14+
continue
15+
}
16+
j++
17+
}
18+
return ans
519
}

leetcode/2401-2500/2410.Maximum-Matching-of-Players-With-Trainers/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
players, trainers []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{4, 7, 9}, []int{8, 2, 5, 8}, 2},
17+
{"TestCase2", []int{1, 1, 1}, []int{10}, 1},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.players, c.trainers)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.players, c.trainers)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)