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 #2165

Merged
merged 1 commit into from
Dec 28, 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
feat: add solutions to lc problem: No.2974
No.2974.Minimum Number Game
  • Loading branch information
yanglbme committed Dec 28, 2023
commit f507bb89715e0617b56cd0399f6851c977d724ec
104 changes: 104 additions & 0 deletions solution/2900-2999/2974.Minimum Number Game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@

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

**方法二:排序 + 交换**

我们可以将数组 $nums$ 排序,然后依次将相邻的两个元素交换位置,即可得到答案数组。

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

<!-- tabs:start -->

### **Python3**
Expand All @@ -73,6 +79,15 @@ class Solution:
return ans
```

```python
class Solution:
def numberGame(self, nums: List[int]) -> List[int]:
nums.sort()
for i in range(0, len(nums), 2):
nums[i], nums[i + 1] = nums[i + 1], nums[i]
return nums
```

### **Java**

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

```java
class Solution {
public int[] numberGame(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length; i += 2) {
int t = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = t;
}
return nums;
}
}
```

### **C++**

```cpp
Expand All @@ -120,6 +149,20 @@ public:
};
```

```cpp
class Solution {
public:
vector<int> numberGame(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
for (int i = 0; i < n; i += 2) {
swap(nums[i], nums[i + 1]);
}
return nums;
}
};
```

### **Go**

```go
Expand Down Expand Up @@ -150,6 +193,16 @@ func (h *hp) Push(x interface{}) {
}
```

```go
func numberGame(nums []int) []int {
sort.Ints(nums)
for i := 0; i < len(nums); i += 2 {
nums[i], nums[i+1] = nums[i+1], nums[i]
}
return nums
}
```

### **TypeScript**

```ts
Expand All @@ -168,6 +221,57 @@ function numberGame(nums: number[]): number[] {
}
```

```ts
function numberGame(nums: number[]): number[] {
nums.sort((a, b) => a - b);
for (let i = 0; i < nums.length; i += 2) {
[nums[i], nums[i + 1]] = [nums[i + 1], nums[i]];
}
return nums;
}
```

### **Rust**

```rust
use std::collections::BinaryHeap;
use std::cmp::Reverse;

impl Solution {
pub fn number_game(nums: Vec<i32>) -> Vec<i32> {
let mut pq = BinaryHeap::new();

for &x in &nums {
pq.push(Reverse(x));
}

let mut ans = Vec::new();

while let Some(Reverse(a)) = pq.pop() {
if let Some(Reverse(b)) = pq.pop() {
ans.push(b);
ans.push(a);
}
}

ans
}
}
```

```rust
impl Solution {
pub fn number_game(nums: Vec<i32>) -> Vec<i32> {
let mut nums = nums;
nums.sort_unstable();
for i in (0..nums.len()).step_by(2) {
nums.swap(i, i + 1);
}
nums
}
}
```

### **...**

```
Expand Down
104 changes: 104 additions & 0 deletions solution/2900-2999/2974.Minimum Number Game/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ We can put the elements in the array $nums$ into a min heap one by one, and each

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

**Solution 2: Sorting + Swapping**

We can sort the array $nums$, and then swap the positions of every two adjacent elements in sequence to get the answer array.

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$.

<!-- tabs:start -->

### **Python3**
Expand All @@ -65,6 +71,15 @@ class Solution:
return ans
```

```python
class Solution:
def numberGame(self, nums: List[int]) -> List[int]:
nums.sort()
for i in range(0, len(nums), 2):
nums[i], nums[i + 1] = nums[i + 1], nums[i]
return nums
```

### **Java**

```java
Expand All @@ -86,6 +101,20 @@ class Solution {
}
```

```java
class Solution {
public int[] numberGame(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length; i += 2) {
int t = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = t;
}
return nums;
}
}
```

### **C++**

```cpp
Expand All @@ -110,6 +139,20 @@ public:
};
```

```cpp
class Solution {
public:
vector<int> numberGame(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
for (int i = 0; i < n; i += 2) {
swap(nums[i], nums[i + 1]);
}
return nums;
}
};
```

### **Go**

```go
Expand Down Expand Up @@ -140,6 +183,16 @@ func (h *hp) Push(x interface{}) {
}
```

```go
func numberGame(nums []int) []int {
sort.Ints(nums)
for i := 0; i < len(nums); i += 2 {
nums[i], nums[i+1] = nums[i+1], nums[i]
}
return nums
}
```

### **TypeScript**

```ts
Expand All @@ -158,6 +211,57 @@ function numberGame(nums: number[]): number[] {
}
```

```ts
function numberGame(nums: number[]): number[] {
nums.sort((a, b) => a - b);
for (let i = 0; i < nums.length; i += 2) {
[nums[i], nums[i + 1]] = [nums[i + 1], nums[i]];
}
return nums;
}
```

### **Rust**

```rust
use std::collections::BinaryHeap;
use std::cmp::Reverse;

impl Solution {
pub fn number_game(nums: Vec<i32>) -> Vec<i32> {
let mut pq = BinaryHeap::new();

for &x in &nums {
pq.push(Reverse(x));
}

let mut ans = Vec::new();

while let Some(Reverse(a)) = pq.pop() {
if let Some(Reverse(b)) = pq.pop() {
ans.push(b);
ans.push(a);
}
}

ans
}
}
```

```rust
impl Solution {
pub fn number_game(nums: Vec<i32>) -> Vec<i32> {
let mut nums = nums;
nums.sort_unstable();
for i in (0..nums.len()).step_by(2) {
nums.swap(i, i + 1);
}
nums
}
}
```

### **...**

```
Expand Down
18 changes: 5 additions & 13 deletions solution/2900-2999/2974.Minimum Number Game/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
class Solution {
public:
vector<int> numberGame(vector<int>& nums) {
priority_queue<int, vector<int>, greater<int>> pq;
for (int x : nums) {
pq.push(x);
sort(nums.begin(), nums.end());
int n = nums.size();
for (int i = 0; i < n; i += 2) {
swap(nums[i], nums[i + 1]);
}
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;
return nums;
}
};
30 changes: 6 additions & 24 deletions solution/2900-2999/2974.Minimum Number Game/Solution.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
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)
func numberGame(nums []int) []int {
sort.Ints(nums)
for i := 0; i < len(nums); i += 2 {
nums[i], nums[i+1] = nums[i+1], nums[i]
}
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))
}
return nums
}
17 changes: 6 additions & 11 deletions solution/2900-2999/2974.Minimum Number Game/Solution.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
class Solution {
public int[] numberGame(int[] nums) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int x : nums) {
pq.offer(x);
Arrays.sort(nums);
for (int i = 0; i < nums.length; i += 2) {
int t = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = t;
}
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;
return nums;
}
}
11 changes: 4 additions & 7 deletions solution/2900-2999/2974.Minimum Number Game/Solution.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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
nums.sort()
for i in range(0, len(nums), 2):
nums[i], nums[i + 1] = nums[i + 1], nums[i]
return nums
10 changes: 10 additions & 0 deletions solution/2900-2999/2974.Minimum Number Game/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
impl Solution {
pub fn number_game(nums: Vec<i32>) -> Vec<i32> {
let mut nums = nums;
nums.sort_unstable();
for i in (0..nums.len()).step_by(2) {
nums.swap(i, i + 1);
}
nums
}
}
Loading