File tree Expand file tree Collapse file tree 3 files changed +67
-0
lines changed
solution/2000-2099/2029.Stone Game IX Expand file tree Collapse file tree 3 files changed +67
-0
lines changed Original file line number Diff line number Diff line change @@ -225,6 +225,30 @@ function stoneGameIX(stones: number[]): boolean {
225
225
}
226
226
```
227
227
228
+ #### JavaScript
229
+
230
+ ``` js
231
+ function stoneGameIX (stones ) {
232
+ const c1 = Array (3 ).fill (0 );
233
+ for (const x of stones) {
234
+ ++ c1[x % 3 ];
235
+ }
236
+ const c2 = [c1[0 ], c1[2 ], c1[1 ]];
237
+ const check = cnt => {
238
+ if (-- cnt[1 ] < 0 ) {
239
+ return false ;
240
+ }
241
+ let r = 1 + Math .min (cnt[1 ], cnt[2 ]) * 2 + cnt[0 ];
242
+ if (cnt[1 ] > cnt[2 ]) {
243
+ -- cnt[1 ];
244
+ ++ r;
245
+ }
246
+ return r % 2 === 1 && cnt[1 ] !== cnt[2 ];
247
+ };
248
+ return check (c1) || check (c2);
249
+ }
250
+ ```
251
+
228
252
<!-- tabs: end -->
229
253
230
254
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -218,6 +218,30 @@ function stoneGameIX(stones: number[]): boolean {
218
218
}
219
219
```
220
220
221
+ #### JavaScript
222
+
223
+ ``` js
224
+ function stoneGameIX (stones ) {
225
+ const c1 = Array (3 ).fill (0 );
226
+ for (const x of stones) {
227
+ ++ c1[x % 3 ];
228
+ }
229
+ const c2 = [c1[0 ], c1[2 ], c1[1 ]];
230
+ const check = cnt => {
231
+ if (-- cnt[1 ] < 0 ) {
232
+ return false ;
233
+ }
234
+ let r = 1 + Math .min (cnt[1 ], cnt[2 ]) * 2 + cnt[0 ];
235
+ if (cnt[1 ] > cnt[2 ]) {
236
+ -- cnt[1 ];
237
+ ++ r;
238
+ }
239
+ return r % 2 === 1 && cnt[1 ] !== cnt[2 ];
240
+ };
241
+ return check (c1) || check (c2);
242
+ }
243
+ ```
244
+
221
245
<!-- tabs: end -->
222
246
223
247
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ function stoneGameIX ( stones ) {
2
+ const c1 = Array ( 3 ) . fill ( 0 ) ;
3
+ for ( const x of stones ) {
4
+ ++ c1 [ x % 3 ] ;
5
+ }
6
+ const c2 = [ c1 [ 0 ] , c1 [ 2 ] , c1 [ 1 ] ] ;
7
+ const check = cnt => {
8
+ if ( -- cnt [ 1 ] < 0 ) {
9
+ return false ;
10
+ }
11
+ let r = 1 + Math . min ( cnt [ 1 ] , cnt [ 2 ] ) * 2 + cnt [ 0 ] ;
12
+ if ( cnt [ 1 ] > cnt [ 2 ] ) {
13
+ -- cnt [ 1 ] ;
14
+ ++ r ;
15
+ }
16
+ return r % 2 === 1 && cnt [ 1 ] !== cnt [ 2 ] ;
17
+ } ;
18
+ return check ( c1 ) || check ( c2 ) ;
19
+ }
You can’t perform that action at this time.
0 commit comments