Skip to content

Commit 1c88423

Browse files
committed
feat: add solutions to lc problem: No.1968
No.1968.Array With Elements Not Equal to Average of Neighbors
1 parent 2dcd8ce commit 1c88423

File tree

8 files changed

+224
-10
lines changed

8 files changed

+224
-10
lines changed

solution/0700-0799/0715.Range Module/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ rangeModule.queryRange(16, 17); 返回 true (尽管执行了删除操作,区
5454

5555
**方法一:线段树**
5656

57-
线段树将整个区间分割为多个不连续的子区间,子区间的数量不超过 `log(width)`。更新某个元素的值,只需要更新 `log(width)` 个区间,并且这些区间都包含在一个包含该元素的大区间内。区间修改时,需要使用**懒标记**保证效率。
57+
线段树将整个区间分割为多个不连续的子区间,子区间的数量不超过 $log(width)$。更新某个元素的值,只需要更新 $log(width)$ 个区间,并且这些区间都包含在一个包含该元素的大区间内。区间修改时,需要使用**懒标记**保证效率。
5858

5959
- 线段树的每个节点代表一个区间;
60-
- 线段树具有唯一的根节点,代表的区间是整个统计范围,如 `[1, N]`
61-
- 线段树的每个叶子节点代表一个长度为 1 的元区间 `[x, x]`
62-
- 对于每个内部节点 `[l, r]`,它的左儿子是 `[l, mid]`,右儿子是 `[mid + 1, r]`, 其中 `mid = ⌊(l + r) / 2⌋` (即向下取整)。
60+
- 线段树具有唯一的根节点,代表的区间是整个统计范围,如 $[1,N]$
61+
- 线段树的每个叶子节点代表一个长度为 $1$ 的元区间 $[x,x]$
62+
- 对于每个内部节点 $[l,r]$,它的左儿子是 $[l,mid]$,右儿子是 $[mid+1,r]$, 其中 $mid=⌊(l+r)/2⌋$ (即向下取整)。
6363

6464
<!-- tabs:start -->
6565

solution/0700-0799/0732.My Calendar III/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,18 @@ myCalendarThree.book(25, 55); // 返回 3
5656
线段树将整个区间分割为多个不连续的子区间,子区间的数量不超过 $log(width)$。更新某个元素的值,只需要更新 $log(width)$ 个区间,并且这些区间都包含在一个包含该元素的大区间内。区间修改时,需要使用**懒标记**保证效率。
5757

5858
- 线段树的每个节点代表一个区间;
59-
- 线段树具有唯一的根节点,代表的区间是整个统计范围,如 $[1, N]$;
59+
- 线段树具有唯一的根节点,代表的区间是整个统计范围,如 $[1,N]$;
6060
- 线段树的每个叶子节点代表一个长度为 $1$ 的元区间 $[x, x]$;
61-
- 对于每个内部节点 $[l, r]$,它的左儿子是 $[l, mid]$,右儿子是 $[mid + 1, r]$, 其中 $mid = ⌊(l + r) / 2⌋$ (即向下取整)。
61+
- 对于每个内部节点 $[l,r]$,它的左儿子是 $[l,mid]$,右儿子是 $[mid+1,r]$, 其中 $mid = ⌊(l+r)/2⌋$ (即向下取整)。
6262

6363
对于本题,线段树节点维护的信息有:
6464

6565
1. 区间范围内被预定的次数的最大值 $v$
6666
1. 懒标记 $add$
6767

68-
由于 $0<=start<end<=109$,时间范围非常大,因此我们采用动态开点。
68+
由于 $0<=start<end<=10^9$,时间范围非常大,因此我们采用动态开点。
6969

70-
时间复杂度 $O(nlogn)$,其中 n 表示日程安排的数量。
70+
时间复杂度 $O(nlogn)$,其中 $n$ 表示日程安排的数量。
7171

7272
<!-- tabs:start -->
7373

solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/README.md

+83-1
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,104 @@ i=3, nums[i] = 2, 两相邻元素平均值为 (6+0) / 2 = 3
4747

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

50+
**方法一:排序**
51+
52+
**方法二:随机打乱**
53+
5054
<!-- tabs:start -->
5155

5256
### **Python3**
5357

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

5660
```python
57-
61+
class Solution:
62+
def rearrangeArray(self, nums: List[int]) -> List[int]:
63+
nums.sort()
64+
n = len(nums)
65+
m = (n + 1) >> 1
66+
ans = []
67+
for i in range(m):
68+
ans.append(nums[i])
69+
if i + m < n:
70+
ans.append(nums[i + m])
71+
return ans
5872
```
5973

6074
### **Java**
6175

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

6478
```java
79+
class Solution {
80+
public int[] rearrangeArray(int[] nums) {
81+
Arrays.sort(nums);
82+
int n = nums.length;
83+
int m = (n + 1) >> 1;
84+
int[] ans = new int[n];
85+
for (int i = 0, j = 0; i < n; i += 2, j++) {
86+
ans[i] = nums[j];
87+
if (j + m < n) {
88+
ans[i + 1] = nums[j + m];
89+
}
90+
}
91+
return ans;
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
vector<int> rearrangeArray(vector<int>& nums) {
102+
sort(nums.begin(), nums.end());
103+
vector<int> ans;
104+
int n = nums.size();
105+
int m = (n + 1) >> 1;
106+
for (int i = 0; i < m; ++i)
107+
{
108+
ans.push_back(nums[i]);
109+
if (i + m < n) ans.push_back(nums[i + m]);
110+
}
111+
return ans;
112+
}
113+
};
114+
```
115+
116+
### **Go**
117+
118+
```go
119+
func rearrangeArray(nums []int) []int {
120+
sort.Ints(nums)
121+
n := len(nums)
122+
m := (n + 1) >> 1
123+
var ans []int
124+
for i := 0; i < m; i++ {
125+
ans = append(ans, nums[i])
126+
if i+m < n {
127+
ans = append(ans, nums[i+m])
128+
}
129+
}
130+
return ans
131+
}
132+
```
65133

134+
```go
135+
func rearrangeArray(nums []int) []int {
136+
rand.Seed(time.Now().UnixNano())
137+
outer:
138+
for {
139+
rand.Shuffle(len(nums), func(i, j int) { nums[i], nums[j] = nums[j], nums[i] })
140+
for i := 1; i < len(nums)-1; i++ {
141+
if nums[i]*2 == nums[i-1]+nums[i+1] {
142+
continue outer
143+
}
144+
}
145+
return nums
146+
}
147+
}
66148
```
67149

68150
### **...**

solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/README_EN.md

+79-1
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,91 @@ When i=3, nums[i] = 2, and the average of its neighbors is (6+0) / 2 = 3.
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def rearrangeArray(self, nums: List[int]) -> List[int]:
53+
nums.sort()
54+
n = len(nums)
55+
m = (n + 1) >> 1
56+
ans = []
57+
for i in range(m):
58+
ans.append(nums[i])
59+
if i + m < n:
60+
ans.append(nums[i + m])
61+
return ans
5262
```
5363

5464
### **Java**
5565

5666
```java
67+
class Solution {
68+
public int[] rearrangeArray(int[] nums) {
69+
Arrays.sort(nums);
70+
int n = nums.length;
71+
int m = (n + 1) >> 1;
72+
int[] ans = new int[n];
73+
for (int i = 0, j = 0; i < n; i += 2, j++) {
74+
ans[i] = nums[j];
75+
if (j + m < n) {
76+
ans[i + 1] = nums[j + m];
77+
}
78+
}
79+
return ans;
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
vector<int> rearrangeArray(vector<int>& nums) {
90+
sort(nums.begin(), nums.end());
91+
vector<int> ans;
92+
int n = nums.size();
93+
int m = (n + 1) >> 1;
94+
for (int i = 0; i < m; ++i)
95+
{
96+
ans.push_back(nums[i]);
97+
if (i + m < n) ans.push_back(nums[i + m]);
98+
}
99+
return ans;
100+
}
101+
};
102+
```
103+
104+
### **Go**
105+
106+
```go
107+
func rearrangeArray(nums []int) []int {
108+
sort.Ints(nums)
109+
n := len(nums)
110+
m := (n + 1) >> 1
111+
var ans []int
112+
for i := 0; i < m; i++ {
113+
ans = append(ans, nums[i])
114+
if i+m < n {
115+
ans = append(ans, nums[i+m])
116+
}
117+
}
118+
return ans
119+
}
120+
```
57121

122+
```go
123+
func rearrangeArray(nums []int) []int {
124+
rand.Seed(time.Now().UnixNano())
125+
outer:
126+
for {
127+
rand.Shuffle(len(nums), func(i, j int) { nums[i], nums[j] = nums[j], nums[i] })
128+
for i := 1; i < len(nums)-1; i++ {
129+
if nums[i]*2 == nums[i-1]+nums[i+1] {
130+
continue outer
131+
}
132+
}
133+
return nums
134+
}
135+
}
58136
```
59137

60138
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
vector<int> rearrangeArray(vector<int>& nums) {
4+
sort(nums.begin(), nums.end());
5+
vector<int> ans;
6+
int n = nums.size();
7+
int m = (n + 1) >> 1;
8+
for (int i = 0; i < m; ++i)
9+
{
10+
ans.push_back(nums[i]);
11+
if (i + m < n) ans.push_back(nums[i + m]);
12+
}
13+
return ans;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func rearrangeArray(nums []int) []int {
2+
sort.Ints(nums)
3+
n := len(nums)
4+
m := (n + 1) >> 1
5+
var ans []int
6+
for i := 0; i < m; i++ {
7+
ans = append(ans, nums[i])
8+
if i+m < n {
9+
ans = append(ans, nums[i+m])
10+
}
11+
}
12+
return ans
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int[] rearrangeArray(int[] nums) {
3+
Arrays.sort(nums);
4+
int n = nums.length;
5+
int m = (n + 1) >> 1;
6+
int[] ans = new int[n];
7+
for (int i = 0, j = 0; i < n; i += 2, j++) {
8+
ans[i] = nums[j];
9+
if (j + m < n) {
10+
ans[i + 1] = nums[j + m];
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def rearrangeArray(self, nums: List[int]) -> List[int]:
3+
nums.sort()
4+
n = len(nums)
5+
m = (n + 1) >> 1
6+
ans = []
7+
for i in range(m):
8+
ans.append(nums[i])
9+
if i + m < n:
10+
ans.append(nums[i + m])
11+
return ans

0 commit comments

Comments
 (0)