File tree 4 files changed +226
-0
lines changed
solution/2600-2699/2601.Prime Subtraction Operation
4 files changed +226
-0
lines changed Original file line number Diff line number Diff line change @@ -274,4 +274,86 @@ function primeSubOperation(nums: number[]): boolean {
274
274
275
275
<!-- solution: end -->
276
276
277
+ <!-- solution: start -->
278
+
279
+ ### 方法二:预处理素数
280
+
281
+ <!-- tabs: start -->
282
+
283
+ #### TypeScript
284
+
285
+ ``` ts
286
+ function primeSubOperation(nums : number []): boolean {
287
+ const p: number [] = [];
288
+ const max = Math .max (... nums );
289
+
290
+ for (let i = 2 ; i < max ; i ++ ) {
291
+ let isPrime = true ;
292
+
293
+ for (const x of p ) {
294
+ if (i % x === 0 ) {
295
+ isPrime = false ;
296
+ break ;
297
+ }
298
+ }
299
+
300
+ while (isPrime && p .length <= i ) {
301
+ p .push (i );
302
+ }
303
+ }
304
+
305
+ for (let i = nums .length - 2 ; i >= 0 ; i -- ) {
306
+ if (nums [i ] < nums [i + 1 ]) continue ;
307
+
308
+ const [x, next] = [nums [i ], nums [i + 1 ]];
309
+ const prime = p [x - next + 1 ];
310
+
311
+ if (! prime || prime >= x ) return false ;
312
+ nums [i ] -= prime ;
313
+ }
314
+
315
+ return true ;
316
+ }
317
+ ```
318
+
319
+ #### JavaScript
320
+
321
+ ``` js
322
+ function primeSubOperation (nums ) {
323
+ const p = [];
324
+ const max = Math .max (... nums);
325
+
326
+ for (let i = 2 ; i < max; i++ ) {
327
+ let isPrime = true ;
328
+
329
+ for (const x of p) {
330
+ if (i % x === 0 ) {
331
+ isPrime = false ;
332
+ break ;
333
+ }
334
+ }
335
+
336
+ while (isPrime && p .length <= i) {
337
+ p .push (i);
338
+ }
339
+ }
340
+
341
+ for (let i = nums .length - 2 ; i >= 0 ; i-- ) {
342
+ if (nums[i] < nums[i + 1 ]) continue ;
343
+
344
+ const [x , next ] = [nums[i], nums[i + 1 ]];
345
+ const prime = p[x - next + 1 ];
346
+
347
+ if (! prime || prime >= x) return false ;
348
+ nums[i] -= prime;
349
+ }
350
+
351
+ return true ;
352
+ }
353
+ ```
354
+
355
+ <!-- tabs: end -->
356
+
357
+ <!-- solution: end -->
358
+
277
359
<!-- problem: end -->
Original file line number Diff line number Diff line change @@ -271,4 +271,86 @@ function primeSubOperation(nums: number[]): boolean {
271
271
272
272
<!-- solution: end -->
273
273
274
+ <!-- solution: start -->
275
+
276
+ ### Solution 2: Preprocessing prime numbers
277
+
278
+ <!-- tabs: start -->
279
+
280
+ #### TypeScript
281
+
282
+ ``` ts
283
+ function primeSubOperation(nums : number []): boolean {
284
+ const p: number [] = [];
285
+ const max = Math .max (... nums );
286
+
287
+ for (let i = 2 ; i < max ; i ++ ) {
288
+ let isPrime = true ;
289
+
290
+ for (const x of p ) {
291
+ if (i % x === 0 ) {
292
+ isPrime = false ;
293
+ break ;
294
+ }
295
+ }
296
+
297
+ while (isPrime && p .length <= i ) {
298
+ p .push (i );
299
+ }
300
+ }
301
+
302
+ for (let i = nums .length - 2 ; i >= 0 ; i -- ) {
303
+ if (nums [i ] < nums [i + 1 ]) continue ;
304
+
305
+ const [x, next] = [nums [i ], nums [i + 1 ]];
306
+ const prime = p [x - next + 1 ];
307
+
308
+ if (! prime || prime >= x ) return false ;
309
+ nums [i ] -= prime ;
310
+ }
311
+
312
+ return true ;
313
+ }
314
+ ```
315
+
316
+ #### JavaScript
317
+
318
+ ``` js
319
+ function primeSubOperation (nums ) {
320
+ const p = [];
321
+ const max = Math .max (... nums);
322
+
323
+ for (let i = 2 ; i < max; i++ ) {
324
+ let isPrime = true ;
325
+
326
+ for (const x of p) {
327
+ if (i % x === 0 ) {
328
+ isPrime = false ;
329
+ break ;
330
+ }
331
+ }
332
+
333
+ while (isPrime && p .length <= i) {
334
+ p .push (i);
335
+ }
336
+ }
337
+
338
+ for (let i = nums .length - 2 ; i >= 0 ; i-- ) {
339
+ if (nums[i] < nums[i + 1 ]) continue ;
340
+
341
+ const [x , next ] = [nums[i], nums[i + 1 ]];
342
+ const prime = p[x - next + 1 ];
343
+
344
+ if (! prime || prime >= x) return false ;
345
+ nums[i] -= prime;
346
+ }
347
+
348
+ return true ;
349
+ }
350
+ ```
351
+
352
+ <!-- tabs: end -->
353
+
354
+ <!-- solution: end -->
355
+
274
356
<!-- problem: end -->
Original file line number Diff line number Diff line change
1
+ function primeSubOperation ( nums ) {
2
+ const p = [ ] ;
3
+ const max = Math . max ( ...nums ) ;
4
+
5
+ for ( let i = 2 ; i < max ; i ++ ) {
6
+ let isPrime = true ;
7
+
8
+ for ( const x of p ) {
9
+ if ( i % x === 0 ) {
10
+ isPrime = false ;
11
+ break ;
12
+ }
13
+ }
14
+
15
+ while ( isPrime && p . length <= i ) {
16
+ p . push ( i ) ;
17
+ }
18
+ }
19
+
20
+ for ( let i = nums . length - 2 ; i >= 0 ; i -- ) {
21
+ if ( nums [ i ] < nums [ i + 1 ] ) continue ;
22
+
23
+ const [ x , next ] = [ nums [ i ] , nums [ i + 1 ] ] ;
24
+ const prime = p [ x - next + 1 ] ;
25
+
26
+ if ( ! prime || prime >= x ) return false ;
27
+ nums [ i ] -= prime ;
28
+ }
29
+
30
+ return true ;
31
+ }
Original file line number Diff line number Diff line change
1
+ function primeSubOperation ( nums : number [ ] ) : boolean {
2
+ const p : number [ ] = [ ] ;
3
+ const max = Math . max ( ...nums ) ;
4
+
5
+ for ( let i = 2 ; i < max ; i ++ ) {
6
+ let isPrime = true ;
7
+
8
+ for ( const x of p ) {
9
+ if ( i % x === 0 ) {
10
+ isPrime = false ;
11
+ break ;
12
+ }
13
+ }
14
+
15
+ while ( isPrime && p . length <= i ) {
16
+ p . push ( i ) ;
17
+ }
18
+ }
19
+
20
+ for ( let i = nums . length - 2 ; i >= 0 ; i -- ) {
21
+ if ( nums [ i ] < nums [ i + 1 ] ) continue ;
22
+
23
+ const [ x , next ] = [ nums [ i ] , nums [ i + 1 ] ] ;
24
+ const prime = p [ x - next + 1 ] ;
25
+
26
+ if ( ! prime || prime >= x ) return false ;
27
+ nums [ i ] -= prime ;
28
+ }
29
+
30
+ return true ;
31
+ }
You can’t perform that action at this time.
0 commit comments