From ffc762e232324f7e22704ea173fda4f520913fe2 Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 12 Aug 2024 21:48:33 +0300 Subject: [PATCH 1/3] feat: add ts solution to lc problem: No.2029 --- .../2000-2099/2029.Stone Game IX/README.md | 42 ++++++++++++++++++- .../2000-2099/2029.Stone Game IX/README_EN.md | 42 ++++++++++++++++++- .../2000-2099/2029.Stone Game IX/Solution2.ts | 23 ++++++++++ 3 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 solution/2000-2099/2029.Stone Game IX/Solution2.ts diff --git a/solution/2000-2099/2029.Stone Game IX/README.md b/solution/2000-2099/2029.Stone Game IX/README.md index a58260bfbd691..976a75d362dc2 100644 --- a/solution/2000-2099/2029.Stone Game IX/README.md +++ b/solution/2000-2099/2029.Stone Game IX/README.md @@ -42,7 +42,7 @@ tags: 输出:true 解释:游戏进行如下: - 回合 1:Alice 可以移除任意一个石子。 -- 回合 2:Bob 移除剩下的石子。 +- 回合 2:Bob 移除剩下的石子。 已移除的石子的值总和为 1 + 2 = 3 且可以被 3 整除。因此,Bob 输,Alice 获胜。 @@ -51,7 +51,7 @@ tags:
 输入:stones = [2]
 输出:false
-解释:Alice 会移除唯一一个石子,已移除石子的值总和为 2 。 
+解释:Alice 会移除唯一一个石子,已移除石子的值总和为 2 。
 由于所有石子都已移除,且值总和无法被 3 整除,Bob 获胜。
 
@@ -229,4 +229,42 @@ function stoneGameIX(stones: number[]): boolean { + + +### Solution 2: Simulation + + + +#### TypeScript + +```ts +function stoneGameIX(stones: number[]): boolean { + if (stones.length === 1) return false; + + const cnt = Array(3).fill(0); + for (const x of stones) cnt[x % 3]++; + + const check = (x: number, cnt: number[]): boolean => { + let c = 1; + if (--cnt[x] < 0) return false; + + while (cnt[1] || cnt[2]) { + if (cnt[x]) { + cnt[x]--; + x = x === 1 ? 2 : 1; + } else return (c + cnt[0]) % 2 === 1; + c++; + } + + return false; + }; + + return check(1, [...cnt]) || check(2, [...cnt]); +} +``` + + + + + diff --git a/solution/2000-2099/2029.Stone Game IX/README_EN.md b/solution/2000-2099/2029.Stone Game IX/README_EN.md index 43242fed1ec31..47be215ae17c3 100644 --- a/solution/2000-2099/2029.Stone Game IX/README_EN.md +++ b/solution/2000-2099/2029.Stone Game IX/README_EN.md @@ -36,7 +36,7 @@ tags: Output: true Explanation: The game will be played as follows: - Turn 1: Alice can remove either stone. -- Turn 2: Bob removes the remaining stone. +- Turn 2: Bob removes the remaining stone. The sum of the removed stones is 1 + 2 = 3 and is divisible by 3. Therefore, Bob loses and Alice wins the game. @@ -45,7 +45,7 @@ The sum of the removed stones is 1 + 2 = 3 and is divisible by 3. Therefore, Bob
 Input: stones = [2]
 Output: false
-Explanation: Alice will remove the only stone, and the sum of the values on the removed stones is 2. 
+Explanation: Alice will remove the only stone, and the sum of the values on the removed stones is 2.
 Since all the stones are removed and the sum of values is not divisible by 3, Bob wins the game.
 
@@ -222,4 +222,42 @@ function stoneGameIX(stones: number[]): boolean { + + +### Solution 2: Simulation + + + +#### TypeScript + +```ts +function stoneGameIX(stones: number[]): boolean { + if (stones.length === 1) return false; + + const cnt = Array(3).fill(0); + for (const x of stones) cnt[x % 3]++; + + const check = (x: number, cnt: number[]): boolean => { + let c = 1; + if (--cnt[x] < 0) return false; + + while (cnt[1] || cnt[2]) { + if (cnt[x]) { + cnt[x]--; + x = x === 1 ? 2 : 1; + } else return (c + cnt[0]) % 2 === 1; + c++; + } + + return false; + }; + + return check(1, [...cnt]) || check(2, [...cnt]); +} +``` + + + + + diff --git a/solution/2000-2099/2029.Stone Game IX/Solution2.ts b/solution/2000-2099/2029.Stone Game IX/Solution2.ts new file mode 100644 index 0000000000000..1e8218c5f0729 --- /dev/null +++ b/solution/2000-2099/2029.Stone Game IX/Solution2.ts @@ -0,0 +1,23 @@ +function stoneGameIX(stones: number[]): boolean { + if (stones.length === 1) return false; + + const cnt = Array(3).fill(0); + for (const x of stones) cnt[x % 3]++; + + const check = (x: number, cnt: number[]): boolean => { + let c = 1; + if (--cnt[x] < 0) return false; + + while (cnt[1] || cnt[2]) { + if (cnt[x]) { + cnt[x]--; + x = x === 1 ? 2 : 1; + } else return (c + cnt[0]) % 2 === 1; + c++; + } + + return false; + }; + + return check(1, [...cnt]) || check(2, [...cnt]); +} From 5a3d18373e1b2a34f9f8c2c9f2d101856ede0b12 Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 12 Aug 2024 21:50:51 +0300 Subject: [PATCH 2/3] feat: add js solution to lc problem: No.2029 --- .../2000-2099/2029.Stone Game IX/README.md | 28 +++++++++++++++++++ .../2000-2099/2029.Stone Game IX/README_EN.md | 28 +++++++++++++++++++ .../2000-2099/2029.Stone Game IX/Solution2.js | 23 +++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 solution/2000-2099/2029.Stone Game IX/Solution2.js diff --git a/solution/2000-2099/2029.Stone Game IX/README.md b/solution/2000-2099/2029.Stone Game IX/README.md index 976a75d362dc2..e0273c5cd8b56 100644 --- a/solution/2000-2099/2029.Stone Game IX/README.md +++ b/solution/2000-2099/2029.Stone Game IX/README.md @@ -263,6 +263,34 @@ function stoneGameIX(stones: number[]): boolean { } ``` +#### JavaScript + +```js +function stoneGameIX(stones) { + if (stones.length === 1) return false; + + const cnt = Array(3).fill(0); + for (const x of stones) cnt[x % 3]++; + + const check = (x, cnt) => { + let c = 1; + if (--cnt[x] < 0) return false; + + while (cnt[1] || cnt[2]) { + if (cnt[x]) { + cnt[x]--; + x = x === 1 ? 2 : 1; + } else return (c + cnt[0]) % 2 === 1; + c++; + } + + return false; + }; + + return check(1, [...cnt]) || check(2, [...cnt]); +} +``` + diff --git a/solution/2000-2099/2029.Stone Game IX/README_EN.md b/solution/2000-2099/2029.Stone Game IX/README_EN.md index 47be215ae17c3..016b557f41e1f 100644 --- a/solution/2000-2099/2029.Stone Game IX/README_EN.md +++ b/solution/2000-2099/2029.Stone Game IX/README_EN.md @@ -256,6 +256,34 @@ function stoneGameIX(stones: number[]): boolean { } ``` +#### JavaScript + +```js +function stoneGameIX(stones) { + if (stones.length === 1) return false; + + const cnt = Array(3).fill(0); + for (const x of stones) cnt[x % 3]++; + + const check = (x, cnt) => { + let c = 1; + if (--cnt[x] < 0) return false; + + while (cnt[1] || cnt[2]) { + if (cnt[x]) { + cnt[x]--; + x = x === 1 ? 2 : 1; + } else return (c + cnt[0]) % 2 === 1; + c++; + } + + return false; + }; + + return check(1, [...cnt]) || check(2, [...cnt]); +} +``` + diff --git a/solution/2000-2099/2029.Stone Game IX/Solution2.js b/solution/2000-2099/2029.Stone Game IX/Solution2.js new file mode 100644 index 0000000000000..562c579fc8a10 --- /dev/null +++ b/solution/2000-2099/2029.Stone Game IX/Solution2.js @@ -0,0 +1,23 @@ +function stoneGameIX(stones) { + if (stones.length === 1) return false; + + const cnt = Array(3).fill(0); + for (const x of stones) cnt[x % 3]++; + + const check = (x, cnt) => { + let c = 1; + if (--cnt[x] < 0) return false; + + while (cnt[1] || cnt[2]) { + if (cnt[x]) { + cnt[x]--; + x = x === 1 ? 2 : 1; + } else return (c + cnt[0]) % 2 === 1; + c++; + } + + return false; + }; + + return check(1, [...cnt]) || check(2, [...cnt]); +} From 2cb05b1dbc80197a7166c069dc858f23027edd51 Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 12 Aug 2024 21:51:59 +0300 Subject: [PATCH 3/3] feat: add js solution to lc problem: No.2029 --- .../2000-2099/2029.Stone Game IX/README.md | 24 +++++++++++++++++++ .../2000-2099/2029.Stone Game IX/README_EN.md | 24 +++++++++++++++++++ .../2000-2099/2029.Stone Game IX/Solution.js | 19 +++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 solution/2000-2099/2029.Stone Game IX/Solution.js diff --git a/solution/2000-2099/2029.Stone Game IX/README.md b/solution/2000-2099/2029.Stone Game IX/README.md index e0273c5cd8b56..474e2578e74d8 100644 --- a/solution/2000-2099/2029.Stone Game IX/README.md +++ b/solution/2000-2099/2029.Stone Game IX/README.md @@ -225,6 +225,30 @@ function stoneGameIX(stones: number[]): boolean { } ``` +#### JavaScript + +```js +function stoneGameIX(stones) { + const c1 = Array(3).fill(0); + for (const x of stones) { + ++c1[x % 3]; + } + const c2 = [c1[0], c1[2], c1[1]]; + const check = cnt => { + if (--cnt[1] < 0) { + return false; + } + let r = 1 + Math.min(cnt[1], cnt[2]) * 2 + cnt[0]; + if (cnt[1] > cnt[2]) { + --cnt[1]; + ++r; + } + return r % 2 === 1 && cnt[1] !== cnt[2]; + }; + return check(c1) || check(c2); +} +``` + diff --git a/solution/2000-2099/2029.Stone Game IX/README_EN.md b/solution/2000-2099/2029.Stone Game IX/README_EN.md index 016b557f41e1f..823e2d2d4ee8d 100644 --- a/solution/2000-2099/2029.Stone Game IX/README_EN.md +++ b/solution/2000-2099/2029.Stone Game IX/README_EN.md @@ -218,6 +218,30 @@ function stoneGameIX(stones: number[]): boolean { } ``` +#### JavaScript + +```js +function stoneGameIX(stones) { + const c1 = Array(3).fill(0); + for (const x of stones) { + ++c1[x % 3]; + } + const c2 = [c1[0], c1[2], c1[1]]; + const check = cnt => { + if (--cnt[1] < 0) { + return false; + } + let r = 1 + Math.min(cnt[1], cnt[2]) * 2 + cnt[0]; + if (cnt[1] > cnt[2]) { + --cnt[1]; + ++r; + } + return r % 2 === 1 && cnt[1] !== cnt[2]; + }; + return check(c1) || check(c2); +} +``` + diff --git a/solution/2000-2099/2029.Stone Game IX/Solution.js b/solution/2000-2099/2029.Stone Game IX/Solution.js new file mode 100644 index 0000000000000..417f2d256177d --- /dev/null +++ b/solution/2000-2099/2029.Stone Game IX/Solution.js @@ -0,0 +1,19 @@ +function stoneGameIX(stones) { + const c1 = Array(3).fill(0); + for (const x of stones) { + ++c1[x % 3]; + } + const c2 = [c1[0], c1[2], c1[1]]; + const check = cnt => { + if (--cnt[1] < 0) { + return false; + } + let r = 1 + Math.min(cnt[1], cnt[2]) * 2 + cnt[0]; + if (cnt[1] > cnt[2]) { + --cnt[1]; + ++r; + } + return r % 2 === 1 && cnt[1] !== cnt[2]; + }; + return check(c1) || check(c2); +}