36
36
<strong >Output:</strong > true
37
37
<strong >Explanation:</strong >  ; The game will be played as follows:
38
38
- Turn 1: Alice can remove either stone.
39
- - Turn 2: Bob removes the remaining stone.
39
+ - Turn 2: Bob removes the remaining stone.
40
40
The sum of the removed stones is 1 + 2 = 3 and is divisible by 3. Therefore, Bob loses and Alice wins the game.
41
41
</pre >
42
42
@@ -45,7 +45,7 @@ The sum of the removed stones is 1 + 2 = 3 and is divisible by 3. Therefore, Bob
45
45
<pre >
46
46
<strong >Input:</strong > stones = [2]
47
47
<strong >Output:</strong > false
48
- <strong >Explanation:</strong >  ; Alice will remove the only stone, and the sum of the values on the removed stones is 2.
48
+ <strong >Explanation:</strong >  ; Alice will remove the only stone, and the sum of the values on the removed stones is 2.
49
49
Since all the stones are removed and the sum of values is not divisible by 3, Bob wins the game.
50
50
</pre >
51
51
@@ -218,6 +218,96 @@ 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
+
245
+ <!-- tabs: end -->
246
+
247
+ <!-- solution: end -->
248
+
249
+ <!-- solution: start -->
250
+
251
+ ### Solution 2: Simulation
252
+
253
+ <!-- tabs: start -->
254
+
255
+ #### TypeScript
256
+
257
+ ``` ts
258
+ function stoneGameIX(stones : number []): boolean {
259
+ if (stones .length === 1 ) return false ;
260
+
261
+ const cnt = Array (3 ).fill (0 );
262
+ for (const x of stones ) cnt [x % 3 ]++ ;
263
+
264
+ const check = (x : number , cnt : number []): boolean => {
265
+ let c = 1 ;
266
+ if (-- cnt [x ] < 0 ) return false ;
267
+
268
+ while (cnt [1 ] || cnt [2 ]) {
269
+ if (cnt [x ]) {
270
+ cnt [x ]-- ;
271
+ x = x === 1 ? 2 : 1 ;
272
+ } else return (c + cnt [0 ]) % 2 === 1 ;
273
+ c ++ ;
274
+ }
275
+
276
+ return false ;
277
+ };
278
+
279
+ return check (1 , [... cnt ]) || check (2 , [... cnt ]);
280
+ }
281
+ ```
282
+
283
+ #### JavaScript
284
+
285
+ ``` js
286
+ function stoneGameIX (stones ) {
287
+ if (stones .length === 1 ) return false ;
288
+
289
+ const cnt = Array (3 ).fill (0 );
290
+ for (const x of stones) cnt[x % 3 ]++ ;
291
+
292
+ const check = (x , cnt ) => {
293
+ let c = 1 ;
294
+ if (-- cnt[x] < 0 ) return false ;
295
+
296
+ while (cnt[1 ] || cnt[2 ]) {
297
+ if (cnt[x]) {
298
+ cnt[x]-- ;
299
+ x = x === 1 ? 2 : 1 ;
300
+ } else return (c + cnt[0 ]) % 2 === 1 ;
301
+ c++ ;
302
+ }
303
+
304
+ return false ;
305
+ };
306
+
307
+ return check (1 , [... cnt]) || check (2 , [... cnt]);
308
+ }
309
+ ```
310
+
221
311
<!-- tabs: end -->
222
312
223
313
<!-- solution: end -->
0 commit comments