Skip to content

Commit ffc762e

Browse files
committed
feat: add ts solution to lc problem: No.2029
1 parent 9488df8 commit ffc762e

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

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

Lines changed: 40 additions & 2 deletions
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

@@ -229,4 +229,42 @@ function stoneGameIX(stones: number[]): boolean {
229229

230230
<!-- solution:end -->
231231

232+
<!-- solution:start -->
233+
234+
### Solution 2: Simulation
235+
236+
<!-- tabs:start -->
237+
238+
#### TypeScript
239+
240+
```ts
241+
function stoneGameIX(stones: number[]): boolean {
242+
if (stones.length === 1) return false;
243+
244+
const cnt = Array(3).fill(0);
245+
for (const x of stones) cnt[x % 3]++;
246+
247+
const check = (x: number, cnt: number[]): boolean => {
248+
let c = 1;
249+
if (--cnt[x] < 0) return false;
250+
251+
while (cnt[1] || cnt[2]) {
252+
if (cnt[x]) {
253+
cnt[x]--;
254+
x = x === 1 ? 2 : 1;
255+
} else return (c + cnt[0]) % 2 === 1;
256+
c++;
257+
}
258+
259+
return false;
260+
};
261+
262+
return check(1, [...cnt]) || check(2, [...cnt]);
263+
}
264+
```
265+
266+
<!-- tabs:end -->
267+
268+
<!-- solution:end -->
269+
232270
<!-- problem:end -->

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

Lines changed: 40 additions & 2 deletions
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

@@ -222,4 +222,42 @@ function stoneGameIX(stones: number[]): boolean {
222222

223223
<!-- solution:end -->
224224

225+
<!-- solution:start -->
226+
227+
### Solution 2: Simulation
228+
229+
<!-- tabs:start -->
230+
231+
#### TypeScript
232+
233+
```ts
234+
function stoneGameIX(stones: number[]): boolean {
235+
if (stones.length === 1) return false;
236+
237+
const cnt = Array(3).fill(0);
238+
for (const x of stones) cnt[x % 3]++;
239+
240+
const check = (x: number, cnt: number[]): boolean => {
241+
let c = 1;
242+
if (--cnt[x] < 0) return false;
243+
244+
while (cnt[1] || cnt[2]) {
245+
if (cnt[x]) {
246+
cnt[x]--;
247+
x = x === 1 ? 2 : 1;
248+
} else return (c + cnt[0]) % 2 === 1;
249+
c++;
250+
}
251+
252+
return false;
253+
};
254+
255+
return check(1, [...cnt]) || check(2, [...cnt]);
256+
}
257+
```
258+
259+
<!-- tabs:end -->
260+
261+
<!-- solution:end -->
262+
225263
<!-- problem:end -->
Lines changed: 23 additions & 0 deletions
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)