Skip to content

Commit aba8dca

Browse files
committed
feat: add weekly contest 479
1 parent c0e6aef commit aba8dca

File tree

29 files changed

+1408
-102
lines changed

29 files changed

+1408
-102
lines changed

solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/README.md

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3766.Mi
157157

158158
### 方法一:预处理 + 二分查找
159159

160-
我们注意到,题目中给定的数字范围仅为 $[1, 5000]$,因此,我们直接预处理 $[0, 2^{14})$ 范围内的所有二进制回文数,并将其存储在一个数组中,记为 $\textit{primes}$。
160+
我们注意到,题目中给定的数字范围仅为 $[1, 5000]$,因此,我们直接预处理 $[0, 2^{14})$ 范围内的所有二进制回文数,并将其存储在一个数组中,记为 $\textit{p}$。
161161

162-
接下来,对于每个数字 $x$,我们使用二分查找在数组 $\textit{primes}$ 中找到第一个大于等于 $x$ 的回文数 $\textit{primes}[i]$,以及第一个小于 $x$ 的回文数 $\textit{primes}[i - 1]$。然后,我们计算将 $x$ 转换为这两个回文数所需的操作次数,并取其中的最小值作为答案。
162+
接下来,对于每个数字 $x$,我们使用二分查找在数组 $\textit{p}$ 中找到第一个大于等于 $x$ 的回文数 $\textit{p}[i]$,以及第一个小于 $x$ 的回文数 $\textit{p}[i - 1]$。然后,我们计算将 $x$ 转换为这两个回文数所需的操作次数,并取其中的最小值作为答案。
163163

164164
时间复杂度 $O(n \times \log M)$,空间复杂度 $O(M)$。其中 $n$ 是数组 $\textit{nums}$ 的长度,而 $M$ 是预处理的二进制回文数的数量。
165165

@@ -168,23 +168,23 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3766.Mi
168168
#### Python3
169169

170170
```python
171-
primes = []
171+
p = []
172172
for i in range(1 << 14):
173173
s = bin(i)[2:]
174174
if s == s[::-1]:
175-
primes.append(i)
175+
p.append(i)
176176

177177

178178
class Solution:
179179
def minOperations(self, nums: List[int]) -> List[int]:
180180
ans = []
181181
for x in nums:
182-
i = bisect_left(primes, x)
182+
i = bisect_left(p, x)
183183
times = inf
184-
if i < len(primes):
185-
times = min(times, primes[i] - x)
184+
if i < len(p):
185+
times = min(times, p[i] - x)
186186
if i >= 1:
187-
times = min(times, x - primes[i - 1])
187+
times = min(times, x - p[i - 1])
188188
ans.append(times)
189189
return ans
190190
```
@@ -193,15 +193,15 @@ class Solution:
193193

194194
```java
195195
class Solution {
196-
private static final List<Integer> primes = new ArrayList<>();
196+
private static final List<Integer> p = new ArrayList<>();
197197

198198
static {
199199
int N = 1 << 14;
200200
for (int i = 0; i < N; i++) {
201201
String s = Integer.toBinaryString(i);
202202
String rs = new StringBuilder(s).reverse().toString();
203203
if (s.equals(rs)) {
204-
primes.add(i);
204+
p.add(i);
205205
}
206206
}
207207
}
@@ -212,23 +212,23 @@ class Solution {
212212
Arrays.fill(ans, Integer.MAX_VALUE);
213213
for (int k = 0; k < n; ++k) {
214214
int x = nums[k];
215-
int i = binarySearch(primes, x);
216-
if (i < primes.size()) {
217-
ans[k] = Math.min(ans[k], primes.get(i) - x);
215+
int i = binarySearch(p, x);
216+
if (i < p.size()) {
217+
ans[k] = Math.min(ans[k], p.get(i) - x);
218218
}
219219
if (i >= 1) {
220-
ans[k] = Math.min(ans[k], x - primes.get(i - 1));
220+
ans[k] = Math.min(ans[k], x - p.get(i - 1));
221221
}
222222
}
223223

224224
return ans;
225225
}
226226

227-
private int binarySearch(List<Integer> primes, int x) {
228-
int l = 0, r = primes.size();
227+
private int binarySearch(List<Integer> p, int x) {
228+
int l = 0, r = p.size();
229229
while (l < r) {
230230
int mid = (l + r) >>> 1;
231-
if (primes.get(mid) >= x) {
231+
if (p.get(mid) >= x) {
232232
r = mid;
233233
} else {
234234
l = mid + 1;
@@ -242,7 +242,7 @@ class Solution {
242242
#### C++
243243

244244
```cpp
245-
vector<int> primes;
245+
vector<int> p;
246246

247247
auto init = [] {
248248
int N = 1 << 14;
@@ -252,7 +252,7 @@ auto init = [] {
252252
string rs = s;
253253
reverse(rs.begin(), rs.end());
254254
if (s == rs) {
255-
primes.push_back(i);
255+
p.push_back(i);
256256
}
257257
}
258258
return 0;
@@ -265,12 +265,12 @@ public:
265265
vector<int> ans(n, INT_MAX);
266266
for (int k = 0; k < n; ++k) {
267267
int x = nums[k];
268-
int i = lower_bound(primes.begin(), primes.end(), x) - primes.begin();
269-
if (i < (int) primes.size()) {
270-
ans[k] = min(ans[k], primes[i] - x);
268+
int i = lower_bound(p.begin(), p.end(), x) - p.begin();
269+
if (i < (int) p.size()) {
270+
ans[k] = min(ans[k], p[i] - x);
271271
}
272272
if (i >= 1) {
273-
ans[k] = min(ans[k], x - primes[i - 1]);
273+
ans[k] = min(ans[k], x - p[i - 1]);
274274
}
275275
}
276276
return ans;
@@ -281,14 +281,14 @@ public:
281281
#### Go
282282
283283
```go
284-
var primes []int
284+
var p []int
285285
286286
func init() {
287287
N := 1 << 14
288288
for i := 0; i < N; i++ {
289289
s := strconv.FormatInt(int64(i), 2)
290290
if isPalindrome(s) {
291-
primes = append(primes, i)
291+
p = append(p, i)
292292
}
293293
}
294294
}
@@ -306,13 +306,13 @@ func isPalindrome(s string) bool {
306306
func minOperations(nums []int) []int {
307307
ans := make([]int, len(nums))
308308
for k, x := range nums {
309-
i := sort.SearchInts(primes, x)
309+
i := sort.SearchInts(p, x)
310310
t := math.MaxInt32
311-
if i < len(primes) {
312-
t = primes[i] - x
311+
if i < len(p) {
312+
t = p[i] - x
313313
}
314314
if i >= 1 {
315-
t = min(t, x-primes[i-1])
315+
t = min(t, x-p[i-1])
316316
}
317317
ans[k] = t
318318
}
@@ -323,7 +323,7 @@ func minOperations(nums []int) []int {
323323
#### TypeScript
324324

325325
```ts
326-
const primes: number[] = (() => {
326+
const p: number[] = (() => {
327327
const res: number[] = [];
328328
const N = 1 << 14;
329329
for (let i = 0; i < N; i++) {
@@ -340,12 +340,12 @@ function minOperations(nums: number[]): number[] {
340340

341341
for (let k = 0; k < nums.length; k++) {
342342
const x = nums[k];
343-
const i = _.sortedIndex(primes, x);
344-
if (i < primes.length) {
345-
ans[k] = primes[i] - x;
343+
const i = _.sortedIndex(p, x);
344+
if (i < p.length) {
345+
ans[k] = p[i] - x;
346346
}
347347
if (i >= 1) {
348-
ans[k] = Math.min(ans[k], x - primes[i - 1]);
348+
ans[k] = Math.min(ans[k], x - p[i - 1]);
349349
}
350350
}
351351

solution/3700-3799/3766.Minimum Operations to Make Binary Palindrome/README_EN.md

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3766.Mi
155155

156156
### Solution 1: Preprocessing + Binary Search
157157

158-
We observe that the range of numbers given in the problem is only $[1, 5000]$. Therefore, we directly preprocess all binary palindromic numbers in the range $[0, 2^{14})$ and store them in an array, denoted as $\textit{primes}$.
158+
We observe that the range of numbers given in the problem is only $[1, 5000]$. Therefore, we directly preprocess all binary palindromic numbers in the range $[0, 2^{14})$ and store them in an array, denoted as $\textit{p}$.
159159

160-
Next, for each number $x$, we use binary search to find the first palindromic number greater than or equal to $x$ in the array $\textit{primes}$, denoted as $\textit{primes}[i]$, as well as the first palindromic number less than $x$, denoted as $\textit{primes}[i - 1]$. Then, we calculate the number of operations required to convert $x$ to these two palindromic numbers and take the minimum value as the answer.
160+
Next, for each number $x$, we use binary search to find the first palindromic number greater than or equal to $x$ in the array $\textit{p}$, denoted as $\textit{p}[i]$, as well as the first palindromic number less than $x$, denoted as $\textit{p}[i - 1]$. Then, we calculate the number of operations required to convert $x$ to these two palindromic numbers and take the minimum value as the answer.
161161

162162
The time complexity is $O(n \times \log M)$, and the space complexity is $O(M)$. Where $n$ is the length of the array $\textit{nums}$, and $M$ is the number of preprocessed binary palindromic numbers.
163163

@@ -166,23 +166,23 @@ The time complexity is $O(n \times \log M)$, and the space complexity is $O(M)$.
166166
#### Python3
167167

168168
```python
169-
primes = []
169+
p = []
170170
for i in range(1 << 14):
171171
s = bin(i)[2:]
172172
if s == s[::-1]:
173-
primes.append(i)
173+
p.append(i)
174174

175175

176176
class Solution:
177177
def minOperations(self, nums: List[int]) -> List[int]:
178178
ans = []
179179
for x in nums:
180-
i = bisect_left(primes, x)
180+
i = bisect_left(p, x)
181181
times = inf
182-
if i < len(primes):
183-
times = min(times, primes[i] - x)
182+
if i < len(p):
183+
times = min(times, p[i] - x)
184184
if i >= 1:
185-
times = min(times, x - primes[i - 1])
185+
times = min(times, x - p[i - 1])
186186
ans.append(times)
187187
return ans
188188
```
@@ -191,15 +191,15 @@ class Solution:
191191

192192
```java
193193
class Solution {
194-
private static final List<Integer> primes = new ArrayList<>();
194+
private static final List<Integer> p = new ArrayList<>();
195195

196196
static {
197197
int N = 1 << 14;
198198
for (int i = 0; i < N; i++) {
199199
String s = Integer.toBinaryString(i);
200200
String rs = new StringBuilder(s).reverse().toString();
201201
if (s.equals(rs)) {
202-
primes.add(i);
202+
p.add(i);
203203
}
204204
}
205205
}
@@ -210,23 +210,23 @@ class Solution {
210210
Arrays.fill(ans, Integer.MAX_VALUE);
211211
for (int k = 0; k < n; ++k) {
212212
int x = nums[k];
213-
int i = binarySearch(primes, x);
214-
if (i < primes.size()) {
215-
ans[k] = Math.min(ans[k], primes.get(i) - x);
213+
int i = binarySearch(p, x);
214+
if (i < p.size()) {
215+
ans[k] = Math.min(ans[k], p.get(i) - x);
216216
}
217217
if (i >= 1) {
218-
ans[k] = Math.min(ans[k], x - primes.get(i - 1));
218+
ans[k] = Math.min(ans[k], x - p.get(i - 1));
219219
}
220220
}
221221

222222
return ans;
223223
}
224224

225-
private int binarySearch(List<Integer> primes, int x) {
226-
int l = 0, r = primes.size();
225+
private int binarySearch(List<Integer> p, int x) {
226+
int l = 0, r = p.size();
227227
while (l < r) {
228228
int mid = (l + r) >>> 1;
229-
if (primes.get(mid) >= x) {
229+
if (p.get(mid) >= x) {
230230
r = mid;
231231
} else {
232232
l = mid + 1;
@@ -240,7 +240,7 @@ class Solution {
240240
#### C++
241241

242242
```cpp
243-
vector<int> primes;
243+
vector<int> p;
244244

245245
auto init = [] {
246246
int N = 1 << 14;
@@ -250,7 +250,7 @@ auto init = [] {
250250
string rs = s;
251251
reverse(rs.begin(), rs.end());
252252
if (s == rs) {
253-
primes.push_back(i);
253+
p.push_back(i);
254254
}
255255
}
256256
return 0;
@@ -263,12 +263,12 @@ public:
263263
vector<int> ans(n, INT_MAX);
264264
for (int k = 0; k < n; ++k) {
265265
int x = nums[k];
266-
int i = lower_bound(primes.begin(), primes.end(), x) - primes.begin();
267-
if (i < (int) primes.size()) {
268-
ans[k] = min(ans[k], primes[i] - x);
266+
int i = lower_bound(p.begin(), p.end(), x) - p.begin();
267+
if (i < (int) p.size()) {
268+
ans[k] = min(ans[k], p[i] - x);
269269
}
270270
if (i >= 1) {
271-
ans[k] = min(ans[k], x - primes[i - 1]);
271+
ans[k] = min(ans[k], x - p[i - 1]);
272272
}
273273
}
274274
return ans;
@@ -279,14 +279,14 @@ public:
279279
#### Go
280280
281281
```go
282-
var primes []int
282+
var p []int
283283
284284
func init() {
285285
N := 1 << 14
286286
for i := 0; i < N; i++ {
287287
s := strconv.FormatInt(int64(i), 2)
288288
if isPalindrome(s) {
289-
primes = append(primes, i)
289+
p = append(p, i)
290290
}
291291
}
292292
}
@@ -304,13 +304,13 @@ func isPalindrome(s string) bool {
304304
func minOperations(nums []int) []int {
305305
ans := make([]int, len(nums))
306306
for k, x := range nums {
307-
i := sort.SearchInts(primes, x)
307+
i := sort.SearchInts(p, x)
308308
t := math.MaxInt32
309-
if i < len(primes) {
310-
t = primes[i] - x
309+
if i < len(p) {
310+
t = p[i] - x
311311
}
312312
if i >= 1 {
313-
t = min(t, x-primes[i-1])
313+
t = min(t, x-p[i-1])
314314
}
315315
ans[k] = t
316316
}
@@ -321,7 +321,7 @@ func minOperations(nums []int) []int {
321321
#### TypeScript
322322

323323
```ts
324-
const primes: number[] = (() => {
324+
const p: number[] = (() => {
325325
const res: number[] = [];
326326
const N = 1 << 14;
327327
for (let i = 0; i < N; i++) {
@@ -338,12 +338,12 @@ function minOperations(nums: number[]): number[] {
338338

339339
for (let k = 0; k < nums.length; k++) {
340340
const x = nums[k];
341-
const i = _.sortedIndex(primes, x);
342-
if (i < primes.length) {
343-
ans[k] = primes[i] - x;
341+
const i = _.sortedIndex(p, x);
342+
if (i < p.length) {
343+
ans[k] = p[i] - x;
344344
}
345345
if (i >= 1) {
346-
ans[k] = Math.min(ans[k], x - primes[i - 1]);
346+
ans[k] = Math.min(ans[k], x - p[i - 1]);
347347
}
348348
}
349349

0 commit comments

Comments
 (0)