Skip to content

Commit 47c1231

Browse files
committed
feat: add solutions to lc problem: No.0932
No.0932.Beautiful Array
1 parent 656bef1 commit 47c1231

File tree

6 files changed

+197
-2
lines changed

6 files changed

+197
-2
lines changed

solution/0900-0999/0932.Beautiful Array/README.md

+78-1
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,99 @@
4343

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

46+
**方法一:分治**
47+
48+
根据题意,漂亮数组 $A$ 需要满足对于任意 $i<k<j$, $A_k*2 \neq A_i+A_j$。
49+
50+
我们可以发现,不等式左侧一定是偶数,那么我们只要保证不等式右侧 $A_i$ 和 $A_j$ 分别是一奇一偶,那么不等式就恒成立。
51+
52+
利用分治,我们将 $n$ 缩小规模为原来的一半,递归调用,可以得到两个漂亮数组 $left$, $right$。我们将 $left$ 中每个元素 $x_i$ 变为 $x_i*2-1$ 可以得到一个奇数数组;将 $right$ 中每个元素 $x_i$ 变为 $x_i*2$,可以得到一个偶数数组。这两个数组仍然是漂亮数组。
53+
54+
> 基于一个性质,将漂亮数组中的每个元素 $x_i$ 变换为 $kx_i+b$,得到的数组仍然是漂亮数组。
55+
56+
将这两个漂亮数组合并在一起,由于满足一奇一偶,那么合并后的数组也是漂亮数组,从而得到了答案。
57+
58+
时间复杂度 $O(nlogn)$。
59+
4660
<!-- tabs:start -->
4761

4862
### **Python3**
4963

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

5266
```python
53-
67+
class Solution:
68+
def beautifulArray(self, n: int) -> List[int]:
69+
if n == 1:
70+
return [1]
71+
left = self.beautifulArray((n + 1) >> 1)
72+
right = self.beautifulArray(n >> 1)
73+
left = [x * 2 - 1 for x in left]
74+
right = [x * 2 for x in right]
75+
return left + right
5476
```
5577

5678
### **Java**
5779

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

6082
```java
83+
class Solution {
84+
public int[] beautifulArray(int n) {
85+
if (n == 1) {
86+
return new int[]{1};
87+
}
88+
int[] left = beautifulArray((n + 1) >> 1);
89+
int[] right = beautifulArray(n >> 1);
90+
int[] ans = new int[n];
91+
int i = 0;
92+
for (int x : left) {
93+
ans[i++] = x * 2 - 1;
94+
}
95+
for (int x : right) {
96+
ans[i++] = x * 2;
97+
}
98+
return ans;
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
vector<int> beautifulArray(int n) {
109+
if (n == 1) return {1};
110+
vector<int> left = beautifulArray((n + 1) >> 1);
111+
vector<int> right = beautifulArray(n >> 1);
112+
vector<int> ans(n);
113+
int i = 0;
114+
for (int& x : left) ans[i++] = x * 2 - 1;
115+
for (int& x : right) ans[i++] = x * 2;
116+
return ans;
117+
}
118+
};
119+
```
61120
121+
### **Go**
122+
123+
```go
124+
func beautifulArray(n int) []int {
125+
if n == 1 {
126+
return []int{1}
127+
}
128+
left := beautifulArray((n + 1) >> 1)
129+
right := beautifulArray(n >> 1)
130+
var ans []int
131+
for _, x := range left {
132+
ans = append(ans, x*2-1)
133+
}
134+
for _, x := range right {
135+
ans = append(ans, x*2)
136+
}
137+
return ans
138+
}
62139
```
63140

64141
### **...**

solution/0900-0999/0932.Beautiful Array/README_EN.md

+64-1
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,76 @@
3535
### **Python3**
3636

3737
```python
38-
38+
class Solution:
39+
def beautifulArray(self, n: int) -> List[int]:
40+
if n == 1:
41+
return [1]
42+
left = self.beautifulArray((n + 1) >> 1)
43+
right = self.beautifulArray(n >> 1)
44+
left = [x * 2 - 1 for x in left]
45+
right = [x * 2 for x in right]
46+
return left + right
3947
```
4048

4149
### **Java**
4250

4351
```java
52+
class Solution {
53+
public int[] beautifulArray(int n) {
54+
if (n == 1) {
55+
return new int[]{1};
56+
}
57+
int[] left = beautifulArray((n + 1) >> 1);
58+
int[] right = beautifulArray(n >> 1);
59+
int[] ans = new int[n];
60+
int i = 0;
61+
for (int x : left) {
62+
ans[i++] = x * 2 - 1;
63+
}
64+
for (int x : right) {
65+
ans[i++] = x * 2;
66+
}
67+
return ans;
68+
}
69+
}
70+
```
71+
72+
### **C++**
73+
74+
```cpp
75+
class Solution {
76+
public:
77+
vector<int> beautifulArray(int n) {
78+
if (n == 1) return {1};
79+
vector<int> left = beautifulArray((n + 1) >> 1);
80+
vector<int> right = beautifulArray(n >> 1);
81+
vector<int> ans(n);
82+
int i = 0;
83+
for (int& x : left) ans[i++] = x * 2 - 1;
84+
for (int& x : right) ans[i++] = x * 2;
85+
return ans;
86+
}
87+
};
88+
```
4489
90+
### **Go**
91+
92+
```go
93+
func beautifulArray(n int) []int {
94+
if n == 1 {
95+
return []int{1}
96+
}
97+
left := beautifulArray((n + 1) >> 1)
98+
right := beautifulArray(n >> 1)
99+
var ans []int
100+
for _, x := range left {
101+
ans = append(ans, x*2-1)
102+
}
103+
for _, x := range right {
104+
ans = append(ans, x*2)
105+
}
106+
return ans
107+
}
45108
```
46109

47110
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
vector<int> beautifulArray(int n) {
4+
if (n == 1) return {1};
5+
vector<int> left = beautifulArray((n + 1) >> 1);
6+
vector<int> right = beautifulArray(n >> 1);
7+
vector<int> ans(n);
8+
int i = 0;
9+
for (int& x : left) ans[i++] = x * 2 - 1;
10+
for (int& x : right) ans[i++] = x * 2;
11+
return ans;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func beautifulArray(n int) []int {
2+
if n == 1 {
3+
return []int{1}
4+
}
5+
left := beautifulArray((n + 1) >> 1)
6+
right := beautifulArray(n >> 1)
7+
var ans []int
8+
for _, x := range left {
9+
ans = append(ans, x*2-1)
10+
}
11+
for _, x := range right {
12+
ans = append(ans, x*2)
13+
}
14+
return ans
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int[] beautifulArray(int n) {
3+
if (n == 1) {
4+
return new int[]{1};
5+
}
6+
int[] left = beautifulArray((n + 1) >> 1);
7+
int[] right = beautifulArray(n >> 1);
8+
int[] ans = new int[n];
9+
int i = 0;
10+
for (int x : left) {
11+
ans[i++] = x * 2 - 1;
12+
}
13+
for (int x : right) {
14+
ans[i++] = x * 2;
15+
}
16+
return ans;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def beautifulArray(self, n: int) -> List[int]:
3+
if n == 1:
4+
return [1]
5+
left = self.beautifulArray((n + 1) >> 1)
6+
right = self.beautifulArray(n >> 1)
7+
left = [x * 2 - 1 for x in left]
8+
right = [x * 2 for x in right]
9+
return left + right

0 commit comments

Comments
 (0)