Skip to content

Commit 5a3d183

Browse files
committed
feat: add js solution to lc problem: No.2029
1 parent ffc762e commit 5a3d183

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

solution/2000-2099/2029.Stone Game IX/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,34 @@ function stoneGameIX(stones: number[]): boolean {
263263
}
264264
```
265265

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+
266294
<!-- tabs:end -->
267295

268296
<!-- solution:end -->

solution/2000-2099/2029.Stone Game IX/README_EN.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,34 @@ function stoneGameIX(stones: number[]): boolean {
256256
}
257257
```
258258

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+
259287
<!-- tabs:end -->
260288

261289
<!-- solution:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}

0 commit comments

Comments
 (0)