Skip to content

Commit 72947dc

Browse files
authored
feat: add js/ts solutions to lc problem: No.2029 (doocs#3405)
1 parent 06d7f60 commit 72947dc

File tree

5 files changed

+249
-4
lines changed

5 files changed

+249
-4
lines changed

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

+92-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ tags:
4242
<strong>输出:</strong>true
4343
<strong>解释:</strong>游戏进行如下:
4444
- 回合 1:Alice 可以移除任意一个石子。
45-
- 回合 2:Bob 移除剩下的石子。
45+
- 回合 2:Bob 移除剩下的石子。
4646
已移除的石子的值总和为 1 + 2 = 3 且可以被 3 整除。因此,Bob 输,Alice 获胜。
4747
</pre>
4848

@@ -51,7 +51,7 @@ tags:
5151
<pre>
5252
<strong>输入:</strong>stones = [2]
5353
<strong>输出:</strong>false
54-
<strong>解释:</strong>Alice 会移除唯一一个石子,已移除石子的值总和为 2 。
54+
<strong>解释:</strong>Alice 会移除唯一一个石子,已移除石子的值总和为 2 。
5555
由于所有石子都已移除,且值总和无法被 3 整除,Bob 获胜。
5656
</pre>
5757

@@ -225,6 +225,96 @@ 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+
252+
<!-- tabs:end -->
253+
254+
<!-- solution:end -->
255+
256+
<!-- solution:start -->
257+
258+
### Solution 2: Simulation
259+
260+
<!-- tabs:start -->
261+
262+
#### TypeScript
263+
264+
```ts
265+
function stoneGameIX(stones: number[]): boolean {
266+
if (stones.length === 1) return false;
267+
268+
const cnt = Array(3).fill(0);
269+
for (const x of stones) cnt[x % 3]++;
270+
271+
const check = (x: number, cnt: number[]): boolean => {
272+
let c = 1;
273+
if (--cnt[x] < 0) return false;
274+
275+
while (cnt[1] || cnt[2]) {
276+
if (cnt[x]) {
277+
cnt[x]--;
278+
x = x === 1 ? 2 : 1;
279+
} else return (c + cnt[0]) % 2 === 1;
280+
c++;
281+
}
282+
283+
return false;
284+
};
285+
286+
return check(1, [...cnt]) || check(2, [...cnt]);
287+
}
288+
```
289+
290+
#### JavaScript
291+
292+
```js
293+
function stoneGameIX(stones) {
294+
if (stones.length === 1) return false;
295+
296+
const cnt = Array(3).fill(0);
297+
for (const x of stones) cnt[x % 3]++;
298+
299+
const check = (x, cnt) => {
300+
let c = 1;
301+
if (--cnt[x] < 0) return false;
302+
303+
while (cnt[1] || cnt[2]) {
304+
if (cnt[x]) {
305+
cnt[x]--;
306+
x = x === 1 ? 2 : 1;
307+
} else return (c + cnt[0]) % 2 === 1;
308+
c++;
309+
}
310+
311+
return false;
312+
};
313+
314+
return check(1, [...cnt]) || check(2, [...cnt]);
315+
}
316+
```
317+
228318
<!-- tabs:end -->
229319

230320
<!-- solution:end -->

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

+92-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ tags:
3636
<strong>Output:</strong> true
3737
<strong>Explanation:</strong>&nbsp;The game will be played as follows:
3838
- Turn 1: Alice can remove either stone.
39-
- Turn 2: Bob removes the remaining stone.
39+
- Turn 2: Bob removes the remaining stone.
4040
The sum of the removed stones is 1 + 2 = 3 and is divisible by 3. Therefore, Bob loses and Alice wins the game.
4141
</pre>
4242

@@ -45,7 +45,7 @@ The sum of the removed stones is 1 + 2 = 3 and is divisible by 3. Therefore, Bob
4545
<pre>
4646
<strong>Input:</strong> stones = [2]
4747
<strong>Output:</strong> false
48-
<strong>Explanation:</strong>&nbsp;Alice will remove the only stone, and the sum of the values on the removed stones is 2.
48+
<strong>Explanation:</strong>&nbsp;Alice will remove the only stone, and the sum of the values on the removed stones is 2.
4949
Since all the stones are removed and the sum of values is not divisible by 3, Bob wins the game.
5050
</pre>
5151

@@ -218,6 +218,96 @@ 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+
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+
221311
<!-- tabs:end -->
222312

223313
<!-- solution:end -->

Diff for: solution/2000-2099/2029.Stone Game IX/Solution.js

+19
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+
}

Diff for: solution/2000-2099/2029.Stone Game IX/Solution2.js

+23
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+
}

Diff for: solution/2000-2099/2029.Stone Game IX/Solution2.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function stoneGameIX(stones: number[]): boolean {
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: number, cnt: number[]): boolean => {
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)