Skip to content

Commit f170776

Browse files
authored
feat: add solutions to lc problem: No.2974 (#2149)
No.2974.Minimum Number Game
1 parent a85f495 commit f170776

File tree

7 files changed

+266
-6
lines changed

7 files changed

+266
-6
lines changed

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

+92-3
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,123 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:模拟 + 优先队列(小根堆)**
53+
54+
我们可以将数组 $nums$ 中的元素依次放入一个小根堆中,每次从小根堆中取出两个元素 $a$ 和 $b$,然后依次将 $b$ 和 $a$ 放入答案数组中,直到小根堆为空。
55+
56+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
57+
5258
<!-- tabs:start -->
5359

5460
### **Python3**
5561

5662
<!-- 这里可写当前语言的特殊实现逻辑 -->
5763

5864
```python
59-
65+
class Solution:
66+
def numberGame(self, nums: List[int]) -> List[int]:
67+
heapify(nums)
68+
ans = []
69+
while nums:
70+
a, b = heappop(nums), heappop(nums)
71+
ans.append(b)
72+
ans.append(a)
73+
return ans
6074
```
6175

6276
### **Java**
6377

6478
<!-- 这里可写当前语言的特殊实现逻辑 -->
6579

6680
```java
67-
81+
class Solution {
82+
public int[] numberGame(int[] nums) {
83+
PriorityQueue<Integer> pq = new PriorityQueue<>();
84+
for (int x : nums) {
85+
pq.offer(x);
86+
}
87+
int[] ans = new int[nums.length];
88+
int i = 0;
89+
while (!pq.isEmpty()) {
90+
int a = pq.poll();
91+
ans[i++] = pq.poll();
92+
ans[i++] = a;
93+
}
94+
return ans;
95+
}
96+
}
6897
```
6998

7099
### **C++**
71100

72101
```cpp
73-
102+
class Solution {
103+
public:
104+
vector<int> numberGame(vector<int>& nums) {
105+
priority_queue<int, vector<int>, greater<int>> pq;
106+
for (int x : nums) {
107+
pq.push(x);
108+
}
109+
vector<int> ans;
110+
while (pq.size()) {
111+
int a = pq.top();
112+
pq.pop();
113+
int b = pq.top();
114+
pq.pop();
115+
ans.push_back(b);
116+
ans.push_back(a);
117+
}
118+
return ans;
119+
}
120+
};
74121
```
75122
76123
### **Go**
77124
78125
```go
126+
func numberGame(nums []int) (ans []int) {
127+
pq := &hp{nums}
128+
heap.Init(pq)
129+
for pq.Len() > 0 {
130+
a := heap.Pop(pq).(int)
131+
b := heap.Pop(pq).(int)
132+
ans = append(ans, b)
133+
ans = append(ans, a)
134+
}
135+
return
136+
}
137+
138+
type hp struct{ sort.IntSlice }
139+
140+
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
141+
func (h *hp) Pop() interface{} {
142+
old := h.IntSlice
143+
n := len(old)
144+
x := old[n-1]
145+
h.IntSlice = old[0 : n-1]
146+
return x
147+
}
148+
func (h *hp) Push(x interface{}) {
149+
h.IntSlice = append(h.IntSlice, x.(int))
150+
}
151+
```
79152

153+
### **TypeScript**
154+
155+
```ts
156+
function numberGame(nums: number[]): number[] {
157+
const pq = new MinPriorityQueue();
158+
for (const x of nums) {
159+
pq.enqueue(x);
160+
}
161+
const ans: number[] = [];
162+
while (pq.size()) {
163+
const a = pq.dequeue().element;
164+
const b = pq.dequeue().element;
165+
ans.push(b, a);
166+
}
167+
return ans;
168+
}
80169
```
81170

82171
### **...**

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

+92-3
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,119 @@ At the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then
4343

4444
## Solutions
4545

46+
**Solution 1: Simulation + Priority Queue (Min Heap)**
47+
48+
We can put the elements in the array $nums$ into a min heap one by one, and each time take out two elements $a$ and $b$ from the min heap, then put $b$ and $a$ into the answer array in turn, until the min heap is empty.
49+
50+
Time complexity is $O(n \times \log n)$, and space complexity is $O(n)$. Where $n$ is the length of the array $nums$.
51+
4652
<!-- tabs:start -->
4753

4854
### **Python3**
4955

5056
```python
51-
57+
class Solution:
58+
def numberGame(self, nums: List[int]) -> List[int]:
59+
heapify(nums)
60+
ans = []
61+
while nums:
62+
a, b = heappop(nums), heappop(nums)
63+
ans.append(b)
64+
ans.append(a)
65+
return ans
5266
```
5367

5468
### **Java**
5569

5670
```java
57-
71+
class Solution {
72+
public int[] numberGame(int[] nums) {
73+
PriorityQueue<Integer> pq = new PriorityQueue<>();
74+
for (int x : nums) {
75+
pq.offer(x);
76+
}
77+
int[] ans = new int[nums.length];
78+
int i = 0;
79+
while (!pq.isEmpty()) {
80+
int a = pq.poll();
81+
ans[i++] = pq.poll();
82+
ans[i++] = a;
83+
}
84+
return ans;
85+
}
86+
}
5887
```
5988

6089
### **C++**
6190

6291
```cpp
63-
92+
class Solution {
93+
public:
94+
vector<int> numberGame(vector<int>& nums) {
95+
priority_queue<int, vector<int>, greater<int>> pq;
96+
for (int x : nums) {
97+
pq.push(x);
98+
}
99+
vector<int> ans;
100+
while (pq.size()) {
101+
int a = pq.top();
102+
pq.pop();
103+
int b = pq.top();
104+
pq.pop();
105+
ans.push_back(b);
106+
ans.push_back(a);
107+
}
108+
return ans;
109+
}
110+
};
64111
```
65112
66113
### **Go**
67114
68115
```go
116+
func numberGame(nums []int) (ans []int) {
117+
pq := &hp{nums}
118+
heap.Init(pq)
119+
for pq.Len() > 0 {
120+
a := heap.Pop(pq).(int)
121+
b := heap.Pop(pq).(int)
122+
ans = append(ans, b)
123+
ans = append(ans, a)
124+
}
125+
return
126+
}
127+
128+
type hp struct{ sort.IntSlice }
129+
130+
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
131+
func (h *hp) Pop() interface{} {
132+
old := h.IntSlice
133+
n := len(old)
134+
x := old[n-1]
135+
h.IntSlice = old[0 : n-1]
136+
return x
137+
}
138+
func (h *hp) Push(x interface{}) {
139+
h.IntSlice = append(h.IntSlice, x.(int))
140+
}
141+
```
69142

143+
### **TypeScript**
144+
145+
```ts
146+
function numberGame(nums: number[]): number[] {
147+
const pq = new MinPriorityQueue();
148+
for (const x of nums) {
149+
pq.enqueue(x);
150+
}
151+
const ans: number[] = [];
152+
while (pq.size()) {
153+
const a = pq.dequeue().element;
154+
const b = pq.dequeue().element;
155+
ans.push(b, a);
156+
}
157+
return ans;
158+
}
70159
```
71160

72161
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<int> numberGame(vector<int>& nums) {
4+
priority_queue<int, vector<int>, greater<int>> pq;
5+
for (int x : nums) {
6+
pq.push(x);
7+
}
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;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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)
9+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int[] numberGame(int[] nums) {
3+
PriorityQueue<Integer> pq = new PriorityQueue<>();
4+
for (int x : nums) {
5+
pq.offer(x);
6+
}
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;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function numberGame(nums: number[]): number[] {
2+
const pq = new MinPriorityQueue();
3+
for (const x of nums) {
4+
pq.enqueue(x);
5+
}
6+
const ans: number[] = [];
7+
while (pq.size()) {
8+
const a = pq.dequeue().element;
9+
const b = pq.dequeue().element;
10+
ans.push(b, a);
11+
}
12+
return ans;
13+
}

0 commit comments

Comments
 (0)