Skip to content

Commit ee243fe

Browse files
committed
feat: add solutions to lc problem: No.1806
No.1806.Minimum Number of Operations to Reinitialize a Permutation
1 parent 8dcb462 commit ee243fe

File tree

6 files changed

+207
-2
lines changed

6 files changed

+207
-2
lines changed

solution/1800-1899/1806.Minimum Number of Operations to Reinitialize a Permutation/README.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,103 @@
6060

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

63+
**方法一:脑筋急转弯**
64+
65+
我们观察数字的变化规律,发现:
66+
67+
1. 新数组的偶数位数字依次是原数组的前半段数字;
68+
1. 新数组的奇数位数字依次是原数组的后半段数字。
69+
70+
即,如果原数组的某个数字下标 $i$ 在 `[0, n >> 1)` 范围内,那么这个数字的新下标就是 `i << 1`;否则,新下标就是 `(i - (n >> 1)) << 1 | 1`
71+
72+
另外,每一轮操作,数字移动的路径都是一样的,只要有一个数字(数字 $0$ 和 $n-1$ 除外)回到了它原来的位置,那么整个序列就和之前的一致了。
73+
74+
因此,我们选择数字 $1$,初始时下标也是 $1$,每次将数字 $1$ 移动到新的位置,直到数字 $1$ 回到原来的位置,就可以得到最小的操作次数。
75+
76+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
77+
6378
<!-- tabs:start -->
6479

6580
### **Python3**
6681

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

6984
```python
70-
85+
class Solution:
86+
def reinitializePermutation(self, n: int) -> int:
87+
ans, i = 0, 1
88+
while 1:
89+
ans += 1
90+
if i < n >> 1:
91+
i <<= 1
92+
else:
93+
i = (i - (n >> 1)) << 1 | 1
94+
if i == 1:
95+
return ans
7196
```
7297

7398
### **Java**
7499

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

77102
```java
103+
class Solution {
104+
public int reinitializePermutation(int n) {
105+
int ans = 0, i = 1;
106+
while (true) {
107+
++ans;
108+
if (i < (n >> 1)) {
109+
i <<= 1;
110+
} else {
111+
i = (i - (n >> 1)) << 1 | 1;
112+
}
113+
if (i == 1) {
114+
return ans;
115+
}
116+
}
117+
}
118+
}
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
class Solution {
125+
public:
126+
int reinitializePermutation(int n) {
127+
int ans = 0, i = 1;
128+
while (1) {
129+
++ans;
130+
if (i < (n >> 1)) {
131+
i <<= 1;
132+
} else {
133+
i = (i - (n >> 1)) << 1 | 1;
134+
}
135+
if (i == 1) {
136+
return ans;
137+
}
138+
}
139+
}
140+
};
141+
```
78142
143+
### **Go**
144+
145+
```go
146+
func reinitializePermutation(n int) int {
147+
ans, i := 0, 1
148+
for {
149+
ans++
150+
if i < (n >> 1) {
151+
i <<= 1
152+
} else {
153+
i = (i-(n>>1))<<1 | 1
154+
}
155+
if i == 1 {
156+
return ans
157+
}
158+
}
159+
}
79160
```
80161

81162
### **...**

solution/1800-1899/1806.Minimum Number of Operations to Reinitialize a Permutation/README_EN.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,79 @@ So it takes only 2 operations.
6161
### **Python3**
6262

6363
```python
64-
64+
class Solution:
65+
def reinitializePermutation(self, n: int) -> int:
66+
ans, i = 0, 1
67+
while 1:
68+
ans += 1
69+
if i < n >> 1:
70+
i <<= 1
71+
else:
72+
i = (i - (n >> 1)) << 1 | 1
73+
if i == 1:
74+
return ans
6575
```
6676

6777
### **Java**
6878

6979
```java
80+
class Solution {
81+
public int reinitializePermutation(int n) {
82+
int ans = 0, i = 1;
83+
while (true) {
84+
++ans;
85+
if (i < (n >> 1)) {
86+
i <<= 1;
87+
} else {
88+
i = (i - (n >> 1)) << 1 | 1;
89+
}
90+
if (i == 1) {
91+
return ans;
92+
}
93+
}
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int reinitializePermutation(int n) {
104+
int ans = 0, i = 1;
105+
while (1) {
106+
++ans;
107+
if (i < (n >> 1)) {
108+
i <<= 1;
109+
} else {
110+
i = (i - (n >> 1)) << 1 | 1;
111+
}
112+
if (i == 1) {
113+
return ans;
114+
}
115+
}
116+
}
117+
};
118+
```
70119
120+
### **Go**
121+
122+
```go
123+
func reinitializePermutation(n int) int {
124+
ans, i := 0, 1
125+
for {
126+
ans++
127+
if i < (n >> 1) {
128+
i <<= 1
129+
} else {
130+
i = (i-(n>>1))<<1 | 1
131+
}
132+
if i == 1 {
133+
return ans
134+
}
135+
}
136+
}
71137
```
72138

73139
### **...**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int reinitializePermutation(int n) {
4+
int ans = 0, i = 1;
5+
while (1) {
6+
++ans;
7+
if (i < (n >> 1)) {
8+
i <<= 1;
9+
} else {
10+
i = (i - (n >> 1)) << 1 | 1;
11+
}
12+
if (i == 1) {
13+
return ans;
14+
}
15+
}
16+
}
17+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func reinitializePermutation(n int) int {
2+
ans, i := 0, 1
3+
for {
4+
ans++
5+
if i < (n >> 1) {
6+
i <<= 1
7+
} else {
8+
i = (i-(n>>1))<<1 | 1
9+
}
10+
if i == 1 {
11+
return ans
12+
}
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int reinitializePermutation(int n) {
3+
int ans = 0, i = 1;
4+
while (true) {
5+
++ans;
6+
if (i < (n >> 1)) {
7+
i <<= 1;
8+
} else {
9+
i = (i - (n >> 1)) << 1 | 1;
10+
}
11+
if (i == 1) {
12+
return ans;
13+
}
14+
}
15+
}
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def reinitializePermutation(self, n: int) -> int:
3+
ans, i = 0, 1
4+
while 1:
5+
ans += 1
6+
if i < n >> 1:
7+
i <<= 1
8+
else:
9+
i = (i - (n >> 1)) << 1 | 1
10+
if i == 1:
11+
return ans

0 commit comments

Comments
 (0)