Skip to content

Commit 2cb05b1

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

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,30 @@ function stoneGameIX(stones: number[]): boolean {
225225
}
226226
```
227227

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+
228252
<!-- tabs:end -->
229253

230254
<!-- solution:end -->

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,30 @@ function stoneGameIX(stones: number[]): boolean {
218218
}
219219
```
220220

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+
221245
<!-- tabs:end -->
222246

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

0 commit comments

Comments
 (0)