Skip to content

Commit ea80b0f

Browse files
authored
Merge pull request #1197 from 0xff-dev/2974
Add solution and test-cases for problem 2974
2 parents 0ea9edb + d5cf49d commit ea80b0f

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

leetcode/2901-3000/2974.Minimum-Number-Game/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [2974.Minimum Number Game][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 `nums` of even length and there is also an empty array `arr`. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows:
5+
6+
- Every round, first Alice will remove the **minimum** element from `nums`, and then Bob does the same.
7+
- Now, first Bob will append the removed element in the array `arr`, and then Alice does the same.
8+
- The game continues until `nums` becomes empty.
9+
10+
Return the resulting array `arr`.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: nums = [5,4,2,3]
16+
Output: [3,2,5,4]
17+
Explanation: In round one, first Alice removes 2 and then Bob removes 3. Then in arr firstly Bob appends 3 and then Alice appends 2. So arr = [3,2].
18+
At the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then Bob removes 5. Then both append in arr which becomes [3,2,5,4].
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Minimum Number Game
23-
```go
2423
```
25-
24+
Input: nums = [2,5]
25+
Output: [5,2]
26+
Explanation: In round one, first Alice removes 2 and then Bob removes 5. Then in arr firstly Bob appends and then Alice appends. So arr = [5,2].
27+
```
2628

2729
## 结语
2830

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int) []int {
6+
sort.Ints(nums)
7+
ans := make([]int, len(nums))
8+
for i := 1; i < len(nums); i += 2 {
9+
ans[i-1] = nums[i]
10+
ans[i] = nums[i-1]
11+
}
12+
return ans
513
}

leetcode/2901-3000/2974.Minimum-Number-Game/Solution_test.go

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

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

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

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

0 commit comments

Comments
 (0)