From f040fb453036f4d1571603973297afcec40ff2c8 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 21 Jun 2025 08:56:36 +0800 Subject: [PATCH] Add solution and test-cases for problem 2410 --- .../README.md | 33 +++++++++++-------- .../Solution.go | 18 ++++++++-- .../Solution_test.go | 21 ++++++------ 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/leetcode/2401-2500/2410.Maximum-Matching-of-Players-With-Trainers/README.md b/leetcode/2401-2500/2410.Maximum-Matching-of-Players-With-Trainers/README.md index 0746f9499..a8e36bff8 100755 --- a/leetcode/2401-2500/2410.Maximum-Matching-of-Players-With-Trainers/README.md +++ b/leetcode/2401-2500/2410.Maximum-Matching-of-Players-With-Trainers/README.md @@ -1,28 +1,33 @@ # [2410.Maximum Matching of Players With Trainers][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## 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: a = "11", b = "1" -Output: "100" +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:** -## 题解 - -### 思路1 -> ... -Maximum Matching of Players With Trainers -```go ``` - +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. +``` ## 结语 diff --git a/leetcode/2401-2500/2410.Maximum-Matching-of-Players-With-Trainers/Solution.go b/leetcode/2401-2500/2410.Maximum-Matching-of-Players-With-Trainers/Solution.go index d115ccf5e..035fc4450 100644 --- a/leetcode/2401-2500/2410.Maximum-Matching-of-Players-With-Trainers/Solution.go +++ b/leetcode/2401-2500/2410.Maximum-Matching-of-Players-With-Trainers/Solution.go @@ -1,5 +1,19 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(players []int, trainers []int) int { + sort.Ints(players) + sort.Ints(trainers) + i, j := 0, 0 + ans := 0 + for i < len(players) && j < len(trainers) { + if trainers[j] >= players[i] { + i, j = i+1, j+1 + ans++ + continue + } + j++ + } + return ans } diff --git a/leetcode/2401-2500/2410.Maximum-Matching-of-Players-With-Trainers/Solution_test.go b/leetcode/2401-2500/2410.Maximum-Matching-of-Players-With-Trainers/Solution_test.go index 14ff50eb4..687609bcf 100644 --- a/leetcode/2401-2500/2410.Maximum-Matching-of-Players-With-Trainers/Solution_test.go +++ b/leetcode/2401-2500/2410.Maximum-Matching-of-Players-With-Trainers/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + players, trainers []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{4, 7, 9}, []int{8, 2, 5, 8}, 2}, + {"TestCase2", []int{1, 1, 1}, []int{10}, 1}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.players, c.trainers) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.players, c.trainers) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }