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.1968 #3548

Merged
merged 1 commit into from
Sep 21, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ i=3, nums[i] = 2, 两相邻元素平均值为 (6+0) / 2 = 3

### 方法一:排序

由于数组中的元素是互不相同的,我们可以先对数组进行排序,然后将数组分成两部分,将前一半的元素放到答案数组中的偶数位置,将后一半的元素放到答案数组中的奇数位置。这样,对于每个元素,它的两个相邻元素都不会等于它的平均值。

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

<!-- tabs:start -->

#### Python3
Expand All @@ -74,7 +78,7 @@ class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
nums.sort()
n = len(nums)
m = (n + 1) >> 1
m = (n + 1) // 2
ans = []
for i in range(m):
ans.append(nums[i])
Expand Down Expand Up @@ -109,13 +113,15 @@ class Solution {
class Solution {
public:
vector<int> rearrangeArray(vector<int>& nums) {
sort(nums.begin(), nums.end());
ranges::sort(nums);
vector<int> ans;
int n = nums.size();
int m = (n + 1) >> 1;
for (int i = 0; i < m; ++i) {
ans.push_back(nums[i]);
if (i + m < n) ans.push_back(nums[i + m]);
if (i + m < n) {
ans.push_back(nums[i + m]);
}
}
return ans;
}
Expand All @@ -125,46 +131,35 @@ public:
#### Go

```go
func rearrangeArray(nums []int) []int {
func rearrangeArray(nums []int) (ans []int) {
sort.Ints(nums)
n := len(nums)
m := (n + 1) >> 1
var ans []int
for i := 0; i < m; i++ {
ans = append(ans, nums[i])
if i+m < n {
ans = append(ans, nums[i+m])
}
}
return ans
return
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二:随机打乱

<!-- tabs:start -->

#### Go

```go
func rearrangeArray(nums []int) []int {
rand.Seed(time.Now().UnixNano())
outer:
for {
rand.Shuffle(len(nums), func(i, j int) { nums[i], nums[j] = nums[j], nums[i] })
for i := 1; i < len(nums)-1; i++ {
if nums[i]*2 == nums[i-1]+nums[i+1] {
continue outer
}
}
return nums
}
#### TypeScript

```ts
function rearrangeArray(nums: number[]): number[] {
nums.sort((a, b) => a - b);
const n = nums.length;
const m = (n + 1) >> 1;
const ans: number[] = [];
for (let i = 0; i < m; i++) {
ans.push(nums[i]);
if (i + m < n) {
ans.push(nums[i + m]);
}
}
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ When i=3, nums[i] = 2, and the average of its neighbors is (6+0) / 2 = 3.

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting

Since the elements in the array are distinct, we can first sort the array, then divide the array into two parts. Place the first half of the elements in the even positions of the answer array, and the second half of the elements in the odd positions of the answer array. In this way, for each element, its two adjacent elements will not be equal to its average value.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.

<!-- tabs:start -->

Expand All @@ -74,7 +78,7 @@ class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
nums.sort()
n = len(nums)
m = (n + 1) >> 1
m = (n + 1) // 2
ans = []
for i in range(m):
ans.append(nums[i])
Expand Down Expand Up @@ -109,13 +113,15 @@ class Solution {
class Solution {
public:
vector<int> rearrangeArray(vector<int>& nums) {
sort(nums.begin(), nums.end());
ranges::sort(nums);
vector<int> ans;
int n = nums.size();
int m = (n + 1) >> 1;
for (int i = 0; i < m; ++i) {
ans.push_back(nums[i]);
if (i + m < n) ans.push_back(nums[i + m]);
if (i + m < n) {
ans.push_back(nums[i + m]);
}
}
return ans;
}
Expand All @@ -125,46 +131,35 @@ public:
#### Go

```go
func rearrangeArray(nums []int) []int {
func rearrangeArray(nums []int) (ans []int) {
sort.Ints(nums)
n := len(nums)
m := (n + 1) >> 1
var ans []int
for i := 0; i < m; i++ {
ans = append(ans, nums[i])
if i+m < n {
ans = append(ans, nums[i+m])
}
}
return ans
return
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Go

```go
func rearrangeArray(nums []int) []int {
rand.Seed(time.Now().UnixNano())
outer:
for {
rand.Shuffle(len(nums), func(i, j int) { nums[i], nums[j] = nums[j], nums[i] })
for i := 1; i < len(nums)-1; i++ {
if nums[i]*2 == nums[i-1]+nums[i+1] {
continue outer
}
}
return nums
}
#### TypeScript

```ts
function rearrangeArray(nums: number[]): number[] {
nums.sort((a, b) => a - b);
const n = nums.length;
const m = (n + 1) >> 1;
const ans: number[] = [];
for (let i = 0; i < m; i++) {
ans.push(nums[i]);
if (i + m < n) {
ans.push(nums[i + m]);
}
}
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
class Solution {
public:
vector<int> rearrangeArray(vector<int>& nums) {
sort(nums.begin(), nums.end());
ranges::sort(nums);
vector<int> ans;
int n = nums.size();
int m = (n + 1) >> 1;
for (int i = 0; i < m; ++i) {
ans.push_back(nums[i]);
if (i + m < n) ans.push_back(nums[i + m]);
if (i + m < n) {
ans.push_back(nums[i + m]);
}
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
func rearrangeArray(nums []int) []int {
func rearrangeArray(nums []int) (ans []int) {
sort.Ints(nums)
n := len(nums)
m := (n + 1) >> 1
var ans []int
for i := 0; i < m; i++ {
ans = append(ans, nums[i])
if i+m < n {
ans = append(ans, nums[i+m])
}
}
return ans
}
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
nums.sort()
n = len(nums)
m = (n + 1) >> 1
m = (n + 1) // 2
ans = []
for i in range(m):
ans.append(nums[i])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function rearrangeArray(nums: number[]): number[] {
nums.sort((a, b) => a - b);
const n = nums.length;
const m = (n + 1) >> 1;
const ans: number[] = [];
for (let i = 0; i < m; i++) {
ans.push(nums[i]);
if (i + m < n) {
ans.push(nums[i + m]);
}
}
return ans;
}

This file was deleted.

Loading