Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2974 #2149

Merged
merged 1 commit into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 92 additions & 3 deletions solution/2900-2999/2974.Minimum Number Game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,123 @@

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

**方法一:模拟 + 优先队列(小根堆)**

我们可以将数组 $nums$ 中的元素依次放入一个小根堆中,每次从小根堆中取出两个元素 $a$ 和 $b$,然后依次将 $b$ 和 $a$ 放入答案数组中,直到小根堆为空。

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

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def numberGame(self, nums: List[int]) -> List[int]:
heapify(nums)
ans = []
while nums:
a, b = heappop(nums), heappop(nums)
ans.append(b)
ans.append(a)
return ans
```

### **Java**

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

```java

class Solution {
public int[] numberGame(int[] nums) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int x : nums) {
pq.offer(x);
}
int[] ans = new int[nums.length];
int i = 0;
while (!pq.isEmpty()) {
int a = pq.poll();
ans[i++] = pq.poll();
ans[i++] = a;
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
vector<int> numberGame(vector<int>& nums) {
priority_queue<int, vector<int>, greater<int>> pq;
for (int x : nums) {
pq.push(x);
}
vector<int> ans;
while (pq.size()) {
int a = pq.top();
pq.pop();
int b = pq.top();
pq.pop();
ans.push_back(b);
ans.push_back(a);
}
return ans;
}
};
```

### **Go**

```go
func numberGame(nums []int) (ans []int) {
pq := &hp{nums}
heap.Init(pq)
for pq.Len() > 0 {
a := heap.Pop(pq).(int)
b := heap.Pop(pq).(int)
ans = append(ans, b)
ans = append(ans, a)
}
return
}

type hp struct{ sort.IntSlice }

func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
func (h *hp) Pop() interface{} {
old := h.IntSlice
n := len(old)
x := old[n-1]
h.IntSlice = old[0 : n-1]
return x
}
func (h *hp) Push(x interface{}) {
h.IntSlice = append(h.IntSlice, x.(int))
}
```

### **TypeScript**

```ts
function numberGame(nums: number[]): number[] {
const pq = new MinPriorityQueue();
for (const x of nums) {
pq.enqueue(x);
}
const ans: number[] = [];
while (pq.size()) {
const a = pq.dequeue().element;
const b = pq.dequeue().element;
ans.push(b, a);
}
return ans;
}
```

### **...**
Expand Down
95 changes: 92 additions & 3 deletions solution/2900-2999/2974.Minimum Number Game/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,119 @@ At the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then

## Solutions

**Solution 1: Simulation + Priority Queue (Min Heap)**

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.

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

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def numberGame(self, nums: List[int]) -> List[int]:
heapify(nums)
ans = []
while nums:
a, b = heappop(nums), heappop(nums)
ans.append(b)
ans.append(a)
return ans
```

### **Java**

```java

class Solution {
public int[] numberGame(int[] nums) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int x : nums) {
pq.offer(x);
}
int[] ans = new int[nums.length];
int i = 0;
while (!pq.isEmpty()) {
int a = pq.poll();
ans[i++] = pq.poll();
ans[i++] = a;
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
vector<int> numberGame(vector<int>& nums) {
priority_queue<int, vector<int>, greater<int>> pq;
for (int x : nums) {
pq.push(x);
}
vector<int> ans;
while (pq.size()) {
int a = pq.top();
pq.pop();
int b = pq.top();
pq.pop();
ans.push_back(b);
ans.push_back(a);
}
return ans;
}
};
```

### **Go**

```go
func numberGame(nums []int) (ans []int) {
pq := &hp{nums}
heap.Init(pq)
for pq.Len() > 0 {
a := heap.Pop(pq).(int)
b := heap.Pop(pq).(int)
ans = append(ans, b)
ans = append(ans, a)
}
return
}

type hp struct{ sort.IntSlice }

func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
func (h *hp) Pop() interface{} {
old := h.IntSlice
n := len(old)
x := old[n-1]
h.IntSlice = old[0 : n-1]
return x
}
func (h *hp) Push(x interface{}) {
h.IntSlice = append(h.IntSlice, x.(int))
}
```

### **TypeScript**

```ts
function numberGame(nums: number[]): number[] {
const pq = new MinPriorityQueue();
for (const x of nums) {
pq.enqueue(x);
}
const ans: number[] = [];
while (pq.size()) {
const a = pq.dequeue().element;
const b = pq.dequeue().element;
ans.push(b, a);
}
return ans;
}
```

### **...**
Expand Down
19 changes: 19 additions & 0 deletions solution/2900-2999/2974.Minimum Number Game/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
vector<int> numberGame(vector<int>& nums) {
priority_queue<int, vector<int>, greater<int>> pq;
for (int x : nums) {
pq.push(x);
}
vector<int> ans;
while (pq.size()) {
int a = pq.top();
pq.pop();
int b = pq.top();
pq.pop();
ans.push_back(b);
ans.push_back(a);
}
return ans;
}
};
25 changes: 25 additions & 0 deletions solution/2900-2999/2974.Minimum Number Game/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
func numberGame(nums []int) (ans []int) {
pq := &hp{nums}
heap.Init(pq)
for pq.Len() > 0 {
a := heap.Pop(pq).(int)
b := heap.Pop(pq).(int)
ans = append(ans, b)
ans = append(ans, a)
}
return
}

type hp struct{ sort.IntSlice }

func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
func (h *hp) Pop() interface{} {
old := h.IntSlice
n := len(old)
x := old[n-1]
h.IntSlice = old[0 : n-1]
return x
}
func (h *hp) Push(x interface{}) {
h.IntSlice = append(h.IntSlice, x.(int))
}
16 changes: 16 additions & 0 deletions solution/2900-2999/2974.Minimum Number Game/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int[] numberGame(int[] nums) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int x : nums) {
pq.offer(x);
}
int[] ans = new int[nums.length];
int i = 0;
while (!pq.isEmpty()) {
int a = pq.poll();
ans[i++] = pq.poll();
ans[i++] = a;
}
return ans;
}
}
9 changes: 9 additions & 0 deletions solution/2900-2999/2974.Minimum Number Game/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def numberGame(self, nums: List[int]) -> List[int]:
heapify(nums)
ans = []
while nums:
a, b = heappop(nums), heappop(nums)
ans.append(b)
ans.append(a)
return ans
13 changes: 13 additions & 0 deletions solution/2900-2999/2974.Minimum Number Game/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function numberGame(nums: number[]): number[] {
const pq = new MinPriorityQueue();
for (const x of nums) {
pq.enqueue(x);
}
const ans: number[] = [];
while (pq.size()) {
const a = pq.dequeue().element;
const b = pq.dequeue().element;
ans.push(b, a);
}
return ans;
}