Skip to content

Commit 05d3b19

Browse files
authored
feat: add solutions to lc problem: No.2974 (doocs#2165)
No.2974.Minimum Number Game
1 parent d3f95fe commit 05d3b19

File tree

8 files changed

+243
-65
lines changed

8 files changed

+243
-65
lines changed

solution/2900-2999/2974.Minimum Number Game/README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@
5555

5656
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
5757

58+
**方法二:排序 + 交换**
59+
60+
我们可以将数组 $nums$ 排序,然后依次将相邻的两个元素交换位置,即可得到答案数组。
61+
62+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 $nums$ 的长度。
63+
5864
<!-- tabs:start -->
5965

6066
### **Python3**
@@ -73,6 +79,15 @@ class Solution:
7379
return ans
7480
```
7581

82+
```python
83+
class Solution:
84+
def numberGame(self, nums: List[int]) -> List[int]:
85+
nums.sort()
86+
for i in range(0, len(nums), 2):
87+
nums[i], nums[i + 1] = nums[i + 1], nums[i]
88+
return nums
89+
```
90+
7691
### **Java**
7792

7893
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -96,6 +111,20 @@ class Solution {
96111
}
97112
```
98113

114+
```java
115+
class Solution {
116+
public int[] numberGame(int[] nums) {
117+
Arrays.sort(nums);
118+
for (int i = 0; i < nums.length; i += 2) {
119+
int t = nums[i];
120+
nums[i] = nums[i + 1];
121+
nums[i + 1] = t;
122+
}
123+
return nums;
124+
}
125+
}
126+
```
127+
99128
### **C++**
100129

101130
```cpp
@@ -120,6 +149,20 @@ public:
120149
};
121150
```
122151
152+
```cpp
153+
class Solution {
154+
public:
155+
vector<int> numberGame(vector<int>& nums) {
156+
sort(nums.begin(), nums.end());
157+
int n = nums.size();
158+
for (int i = 0; i < n; i += 2) {
159+
swap(nums[i], nums[i + 1]);
160+
}
161+
return nums;
162+
}
163+
};
164+
```
165+
123166
### **Go**
124167

125168
```go
@@ -150,6 +193,16 @@ func (h *hp) Push(x interface{}) {
150193
}
151194
```
152195

196+
```go
197+
func numberGame(nums []int) []int {
198+
sort.Ints(nums)
199+
for i := 0; i < len(nums); i += 2 {
200+
nums[i], nums[i+1] = nums[i+1], nums[i]
201+
}
202+
return nums
203+
}
204+
```
205+
153206
### **TypeScript**
154207

155208
```ts
@@ -168,6 +221,57 @@ function numberGame(nums: number[]): number[] {
168221
}
169222
```
170223

224+
```ts
225+
function numberGame(nums: number[]): number[] {
226+
nums.sort((a, b) => a - b);
227+
for (let i = 0; i < nums.length; i += 2) {
228+
[nums[i], nums[i + 1]] = [nums[i + 1], nums[i]];
229+
}
230+
return nums;
231+
}
232+
```
233+
234+
### **Rust**
235+
236+
```rust
237+
use std::collections::BinaryHeap;
238+
use std::cmp::Reverse;
239+
240+
impl Solution {
241+
pub fn number_game(nums: Vec<i32>) -> Vec<i32> {
242+
let mut pq = BinaryHeap::new();
243+
244+
for &x in &nums {
245+
pq.push(Reverse(x));
246+
}
247+
248+
let mut ans = Vec::new();
249+
250+
while let Some(Reverse(a)) = pq.pop() {
251+
if let Some(Reverse(b)) = pq.pop() {
252+
ans.push(b);
253+
ans.push(a);
254+
}
255+
}
256+
257+
ans
258+
}
259+
}
260+
```
261+
262+
```rust
263+
impl Solution {
264+
pub fn number_game(nums: Vec<i32>) -> Vec<i32> {
265+
let mut nums = nums;
266+
nums.sort_unstable();
267+
for i in (0..nums.len()).step_by(2) {
268+
nums.swap(i, i + 1);
269+
}
270+
nums
271+
}
272+
}
273+
```
274+
171275
### **...**
172276

173277
```

solution/2900-2999/2974.Minimum Number Game/README_EN.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ We can put the elements in the array $nums$ into a min heap one by one, and each
4949

5050
Time complexity is $O(n \times \log n)$, and space complexity is $O(n)$. Where $n$ is the length of the array $nums$.
5151

52+
**Solution 2: Sorting + Swapping**
53+
54+
We can sort the array $nums$, and then swap the positions of every two adjacent elements in sequence to get the answer array.
55+
56+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Where $n$ is the length of the array $nums$.
57+
5258
<!-- tabs:start -->
5359

5460
### **Python3**
@@ -65,6 +71,15 @@ class Solution:
6571
return ans
6672
```
6773

74+
```python
75+
class Solution:
76+
def numberGame(self, nums: List[int]) -> List[int]:
77+
nums.sort()
78+
for i in range(0, len(nums), 2):
79+
nums[i], nums[i + 1] = nums[i + 1], nums[i]
80+
return nums
81+
```
82+
6883
### **Java**
6984

7085
```java
@@ -86,6 +101,20 @@ class Solution {
86101
}
87102
```
88103

104+
```java
105+
class Solution {
106+
public int[] numberGame(int[] nums) {
107+
Arrays.sort(nums);
108+
for (int i = 0; i < nums.length; i += 2) {
109+
int t = nums[i];
110+
nums[i] = nums[i + 1];
111+
nums[i + 1] = t;
112+
}
113+
return nums;
114+
}
115+
}
116+
```
117+
89118
### **C++**
90119

91120
```cpp
@@ -110,6 +139,20 @@ public:
110139
};
111140
```
112141
142+
```cpp
143+
class Solution {
144+
public:
145+
vector<int> numberGame(vector<int>& nums) {
146+
sort(nums.begin(), nums.end());
147+
int n = nums.size();
148+
for (int i = 0; i < n; i += 2) {
149+
swap(nums[i], nums[i + 1]);
150+
}
151+
return nums;
152+
}
153+
};
154+
```
155+
113156
### **Go**
114157

115158
```go
@@ -140,6 +183,16 @@ func (h *hp) Push(x interface{}) {
140183
}
141184
```
142185

186+
```go
187+
func numberGame(nums []int) []int {
188+
sort.Ints(nums)
189+
for i := 0; i < len(nums); i += 2 {
190+
nums[i], nums[i+1] = nums[i+1], nums[i]
191+
}
192+
return nums
193+
}
194+
```
195+
143196
### **TypeScript**
144197

145198
```ts
@@ -158,6 +211,57 @@ function numberGame(nums: number[]): number[] {
158211
}
159212
```
160213

214+
```ts
215+
function numberGame(nums: number[]): number[] {
216+
nums.sort((a, b) => a - b);
217+
for (let i = 0; i < nums.length; i += 2) {
218+
[nums[i], nums[i + 1]] = [nums[i + 1], nums[i]];
219+
}
220+
return nums;
221+
}
222+
```
223+
224+
### **Rust**
225+
226+
```rust
227+
use std::collections::BinaryHeap;
228+
use std::cmp::Reverse;
229+
230+
impl Solution {
231+
pub fn number_game(nums: Vec<i32>) -> Vec<i32> {
232+
let mut pq = BinaryHeap::new();
233+
234+
for &x in &nums {
235+
pq.push(Reverse(x));
236+
}
237+
238+
let mut ans = Vec::new();
239+
240+
while let Some(Reverse(a)) = pq.pop() {
241+
if let Some(Reverse(b)) = pq.pop() {
242+
ans.push(b);
243+
ans.push(a);
244+
}
245+
}
246+
247+
ans
248+
}
249+
}
250+
```
251+
252+
```rust
253+
impl Solution {
254+
pub fn number_game(nums: Vec<i32>) -> Vec<i32> {
255+
let mut nums = nums;
256+
nums.sort_unstable();
257+
for i in (0..nums.len()).step_by(2) {
258+
nums.swap(i, i + 1);
259+
}
260+
nums
261+
}
262+
}
263+
```
264+
161265
### **...**
162266

163267
```
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
class Solution {
22
public:
33
vector<int> numberGame(vector<int>& nums) {
4-
priority_queue<int, vector<int>, greater<int>> pq;
5-
for (int x : nums) {
6-
pq.push(x);
4+
sort(nums.begin(), nums.end());
5+
int n = nums.size();
6+
for (int i = 0; i < n; i += 2) {
7+
swap(nums[i], nums[i + 1]);
78
}
8-
vector<int> ans;
9-
while (pq.size()) {
10-
int a = pq.top();
11-
pq.pop();
12-
int b = pq.top();
13-
pq.pop();
14-
ans.push_back(b);
15-
ans.push_back(a);
16-
}
17-
return ans;
9+
return nums;
1810
}
1911
};
Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
1-
func numberGame(nums []int) (ans []int) {
2-
pq := &hp{nums}
3-
heap.Init(pq)
4-
for pq.Len() > 0 {
5-
a := heap.Pop(pq).(int)
6-
b := heap.Pop(pq).(int)
7-
ans = append(ans, b)
8-
ans = append(ans, a)
1+
func numberGame(nums []int) []int {
2+
sort.Ints(nums)
3+
for i := 0; i < len(nums); i += 2 {
4+
nums[i], nums[i+1] = nums[i+1], nums[i]
95
}
10-
return
11-
}
12-
13-
type hp struct{ sort.IntSlice }
14-
15-
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
16-
func (h *hp) Pop() interface{} {
17-
old := h.IntSlice
18-
n := len(old)
19-
x := old[n-1]
20-
h.IntSlice = old[0 : n-1]
21-
return x
22-
}
23-
func (h *hp) Push(x interface{}) {
24-
h.IntSlice = append(h.IntSlice, x.(int))
25-
}
6+
return nums
7+
}
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
class Solution {
22
public int[] numberGame(int[] nums) {
3-
PriorityQueue<Integer> pq = new PriorityQueue<>();
4-
for (int x : nums) {
5-
pq.offer(x);
3+
Arrays.sort(nums);
4+
for (int i = 0; i < nums.length; i += 2) {
5+
int t = nums[i];
6+
nums[i] = nums[i + 1];
7+
nums[i + 1] = t;
68
}
7-
int[] ans = new int[nums.length];
8-
int i = 0;
9-
while (!pq.isEmpty()) {
10-
int a = pq.poll();
11-
ans[i++] = pq.poll();
12-
ans[i++] = a;
13-
}
14-
return ans;
9+
return nums;
1510
}
1611
}
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
class Solution:
22
def numberGame(self, nums: List[int]) -> List[int]:
3-
heapify(nums)
4-
ans = []
5-
while nums:
6-
a, b = heappop(nums), heappop(nums)
7-
ans.append(b)
8-
ans.append(a)
9-
return ans
3+
nums.sort()
4+
for i in range(0, len(nums), 2):
5+
nums[i], nums[i + 1] = nums[i + 1], nums[i]
6+
return nums
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
impl Solution {
2+
pub fn number_game(nums: Vec<i32>) -> Vec<i32> {
3+
let mut nums = nums;
4+
nums.sort_unstable();
5+
for i in (0..nums.len()).step_by(2) {
6+
nums.swap(i, i + 1);
7+
}
8+
nums
9+
}
10+
}

0 commit comments

Comments
 (0)