diff --git a/leetcode/2901-3000/2974.Minimum-Number-Game/README.md b/leetcode/2901-3000/2974.Minimum-Number-Game/README.md index dbf7beab5..07b935522 100755 --- a/leetcode/2901-3000/2974.Minimum-Number-Game/README.md +++ b/leetcode/2901-3000/2974.Minimum-Number-Game/README.md @@ -1,28 +1,30 @@ # [2974.Minimum Number Game][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 `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: + +- Every round, first Alice will remove the **minimum** element from `nums`, and then Bob does the same. +- Now, first Bob will append the removed element in the array `arr`, and then Alice does the same. +- The game continues until `nums` becomes empty. + +Return the resulting array `arr`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [5,4,2,3] +Output: [3,2,5,4] +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]. +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]. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Minimum Number Game -```go ``` - +Input: nums = [2,5] +Output: [5,2] +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]. +``` ## 结语 diff --git a/leetcode/2901-3000/2974.Minimum-Number-Game/Solution.go b/leetcode/2901-3000/2974.Minimum-Number-Game/Solution.go index d115ccf5e..829b9e385 100644 --- a/leetcode/2901-3000/2974.Minimum-Number-Game/Solution.go +++ b/leetcode/2901-3000/2974.Minimum-Number-Game/Solution.go @@ -1,5 +1,13 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(nums []int) []int { + sort.Ints(nums) + ans := make([]int, len(nums)) + for i := 1; i < len(nums); i += 2 { + ans[i-1] = nums[i] + ans[i] = nums[i-1] + } + return ans } diff --git a/leetcode/2901-3000/2974.Minimum-Number-Game/Solution_test.go b/leetcode/2901-3000/2974.Minimum-Number-Game/Solution_test.go index 14ff50eb4..7edaab4f0 100644 --- a/leetcode/2901-3000/2974.Minimum-Number-Game/Solution_test.go +++ b/leetcode/2901-3000/2974.Minimum-Number-Game/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{5, 4, 2, 3}, []int{3, 2, 5, 4}}, + {"TestCase2", []int{2, 5}, []int{5, 2}}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }