File tree Expand file tree Collapse file tree 3 files changed +79
-0
lines changed
solution/2000-2099/2029.Stone Game IX Expand file tree Collapse file tree 3 files changed +79
-0
lines changed Original file line number Diff line number Diff line change @@ -263,6 +263,34 @@ function stoneGameIX(stones: number[]): boolean {
263
263
}
264
264
```
265
265
266
+ #### JavaScript
267
+
268
+ ``` js
269
+ function stoneGameIX (stones ) {
270
+ if (stones .length === 1 ) return false ;
271
+
272
+ const cnt = Array (3 ).fill (0 );
273
+ for (const x of stones) cnt[x % 3 ]++ ;
274
+
275
+ const check = (x , cnt ) => {
276
+ let c = 1 ;
277
+ if (-- cnt[x] < 0 ) return false ;
278
+
279
+ while (cnt[1 ] || cnt[2 ]) {
280
+ if (cnt[x]) {
281
+ cnt[x]-- ;
282
+ x = x === 1 ? 2 : 1 ;
283
+ } else return (c + cnt[0 ]) % 2 === 1 ;
284
+ c++ ;
285
+ }
286
+
287
+ return false ;
288
+ };
289
+
290
+ return check (1 , [... cnt]) || check (2 , [... cnt]);
291
+ }
292
+ ```
293
+
266
294
<!-- tabs: end -->
267
295
268
296
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -256,6 +256,34 @@ function stoneGameIX(stones: number[]): boolean {
256
256
}
257
257
```
258
258
259
+ #### JavaScript
260
+
261
+ ``` js
262
+ function stoneGameIX (stones ) {
263
+ if (stones .length === 1 ) return false ;
264
+
265
+ const cnt = Array (3 ).fill (0 );
266
+ for (const x of stones) cnt[x % 3 ]++ ;
267
+
268
+ const check = (x , cnt ) => {
269
+ let c = 1 ;
270
+ if (-- cnt[x] < 0 ) return false ;
271
+
272
+ while (cnt[1 ] || cnt[2 ]) {
273
+ if (cnt[x]) {
274
+ cnt[x]-- ;
275
+ x = x === 1 ? 2 : 1 ;
276
+ } else return (c + cnt[0 ]) % 2 === 1 ;
277
+ c++ ;
278
+ }
279
+
280
+ return false ;
281
+ };
282
+
283
+ return check (1 , [... cnt]) || check (2 , [... cnt]);
284
+ }
285
+ ```
286
+
259
287
<!-- tabs: end -->
260
288
261
289
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ function stoneGameIX ( stones ) {
2
+ if ( stones . length === 1 ) return false ;
3
+
4
+ const cnt = Array ( 3 ) . fill ( 0 ) ;
5
+ for ( const x of stones ) cnt [ x % 3 ] ++ ;
6
+
7
+ const check = ( x , cnt ) => {
8
+ let c = 1 ;
9
+ if ( -- cnt [ x ] < 0 ) return false ;
10
+
11
+ while ( cnt [ 1 ] || cnt [ 2 ] ) {
12
+ if ( cnt [ x ] ) {
13
+ cnt [ x ] -- ;
14
+ x = x === 1 ? 2 : 1 ;
15
+ } else return ( c + cnt [ 0 ] ) % 2 === 1 ;
16
+ c ++ ;
17
+ }
18
+
19
+ return false ;
20
+ } ;
21
+
22
+ return check ( 1 , [ ...cnt ] ) || check ( 2 , [ ...cnt ] ) ;
23
+ }
You can’t perform that action at this time.
0 commit comments