From 718bd8dea220b5569386a4a0803f5de85909b5d3 Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 8 Jul 2024 10:59:20 +0300 Subject: [PATCH 1/8] feat: update ts solution to lc problem: No.1823 --- .../README.md | 36 ++----------------- .../README_EN.md | 36 ++----------------- .../Solution.ts | 36 ++----------------- 3 files changed, 9 insertions(+), 99 deletions(-) diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md b/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md index 185dc5936224e..8812d4fedbf81 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md @@ -140,40 +140,10 @@ func findTheWinner(n int, k int) int { #### TypeScript ```ts -class LinkNode { - public val: number; - public next: LinkNode; - - constructor(val: number = 0, next?: LinkNode) { - this.val = val; - this.next = next; - } -} - function findTheWinner(n: number, k: number): number { - if (k === 1) { - return n; - } - const dummy = new LinkNode(0); - let cur = dummy; - for (let i = 1; i <= n; i++) { - cur.next = new LinkNode(i); - cur = cur.next; - } - cur.next = dummy.next; - - cur = dummy; - let count = 0; - while (cur.next != cur) { - count++; - if (count === k) { - cur.next = cur.next.next; - count = 0; - } else { - cur = cur.next; - } - } - return cur.val; + if (n === 1) return 1; + const res = (findTheWinner(n - 1, k) + k) % n; + return res ? res : n; } ``` diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md b/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md index 800d1682c5d8f..712b8b98fc761 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md @@ -139,40 +139,10 @@ func findTheWinner(n int, k int) int { #### TypeScript ```ts -class LinkNode { - public val: number; - public next: LinkNode; - - constructor(val: number = 0, next?: LinkNode) { - this.val = val; - this.next = next; - } -} - function findTheWinner(n: number, k: number): number { - if (k === 1) { - return n; - } - const dummy = new LinkNode(0); - let cur = dummy; - for (let i = 1; i <= n; i++) { - cur.next = new LinkNode(i); - cur = cur.next; - } - cur.next = dummy.next; - - cur = dummy; - let count = 0; - while (cur.next != cur) { - count++; - if (count === k) { - cur.next = cur.next.next; - count = 0; - } else { - cur = cur.next; - } - } - return cur.val; + if (n === 1) return 1; + const res = (findTheWinner(n - 1, k) + k) % n; + return res ? res : n; } ``` diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.ts b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.ts index 047f95c64859c..8f8e8e3c70b8c 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.ts +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.ts @@ -1,35 +1,5 @@ -class LinkNode { - public val: number; - public next: LinkNode; - - constructor(val: number = 0, next?: LinkNode) { - this.val = val; - this.next = next; - } -} - function findTheWinner(n: number, k: number): number { - if (k === 1) { - return n; - } - const dummy = new LinkNode(0); - let cur = dummy; - for (let i = 1; i <= n; i++) { - cur.next = new LinkNode(i); - cur = cur.next; - } - cur.next = dummy.next; - - cur = dummy; - let count = 0; - while (cur.next != cur) { - count++; - if (count === k) { - cur.next = cur.next.next; - count = 0; - } else { - cur = cur.next; - } - } - return cur.val; + if (n === 1) return 1; + const res = (findTheWinner(n - 1, k) + k) % n; + return res ? res : n; } From aa2fb2e5ecf43028f94bb0cf62772754f10837cf Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 8 Jul 2024 11:00:44 +0300 Subject: [PATCH 2/8] feat: add js solution to lc problem: No.1823 --- .../README.md | 10 ++++++++++ .../README_EN.md | 10 ++++++++++ .../Solution.js | 5 +++++ 3 files changed, 25 insertions(+) create mode 100644 solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.js diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md b/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md index 8812d4fedbf81..597055c400f3a 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md @@ -147,6 +147,16 @@ function findTheWinner(n: number, k: number): number { } ``` +#### JavaScript + +```js +function findTheWinner(n, k) { + if (n === 1) return 1; + const res = (findTheWinner(n - 1, k) + k) % n; + return res ? res : n; +} +``` + diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md b/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md index 712b8b98fc761..c00f3b3162197 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md @@ -146,6 +146,16 @@ function findTheWinner(n: number, k: number): number { } ``` +#### JavaScript + +```js +function findTheWinner(n, k) { + if (n === 1) return 1; + const res = (findTheWinner(n - 1, k) + k) % n; + return res ? res : n; +} +``` + diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.js b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.js new file mode 100644 index 0000000000000..63815f88d8317 --- /dev/null +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.js @@ -0,0 +1,5 @@ +function findTheWinner(n, k) { + if (n === 1) return 1; + const res = (findTheWinner(n - 1, k) + k) % n; + return res ? res : n; +} From 08801234e30268696da4769f3075987b8c78114e Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 8 Jul 2024 11:05:19 +0300 Subject: [PATCH 3/8] feat: add ts solution to lc problem: No.1823 --- .../README.md | 26 +++++++++++++++++++ .../README_EN.md | 26 +++++++++++++++++++ .../Solution2.ts | 11 ++++++++ 3 files changed, 63 insertions(+) create mode 100644 solution/1800-1899/1823.Find the Winner of the Circular Game/Solution2.ts diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md b/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md index 597055c400f3a..35a7bd78b34c3 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md @@ -161,4 +161,30 @@ function findTheWinner(n, k) { + + +### Solution 2. Simulation + + + +#### TypeScript + +```ts +function findTheWinner(n: number, k: number): number { + const arr = Array.from({ length: n }, (_, i) => i + 1); + let i = 0; + + while (arr.length > 1) { + i = (i + k - 1) % arr.length; + arr.splice(i, 1); + } + + return arr[0]; +} +``` + + + + + diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md b/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md index c00f3b3162197..748158d329787 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md @@ -160,4 +160,30 @@ function findTheWinner(n, k) { + + +### Solution 2. Simulation + + + +#### TypeScript + +```ts +function findTheWinner(n: number, k: number): number { + const arr = Array.from({ length: n }, (_, i) => i + 1); + let i = 0; + + while (arr.length > 1) { + i = (i + k - 1) % arr.length; + arr.splice(i, 1); + } + + return arr[0]; +} +``` + + + + + diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution2.ts b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution2.ts new file mode 100644 index 0000000000000..a94ffa41d294b --- /dev/null +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution2.ts @@ -0,0 +1,11 @@ +function findTheWinner(n: number, k: number): number { + const arr = Array.from({ length: n }, (_, i) => i + 1); + let i = 0; + + while (arr.length > 1) { + i = (i + k - 1) % arr.length; + arr.splice(i, 1); + } + + return arr[0]; +} From 75a57e9d5ed605b68832590e1a179293d5948d2b Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 8 Jul 2024 11:07:20 +0300 Subject: [PATCH 4/8] feat: add js solution to lc problem: No.1823 --- .../README.md | 16 ++++++++++++++++ .../README_EN.md | 16 ++++++++++++++++ .../Solution2.js | 11 +++++++++++ 3 files changed, 43 insertions(+) create mode 100644 solution/1800-1899/1823.Find the Winner of the Circular Game/Solution2.js diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md b/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md index 35a7bd78b34c3..a23d432282e3c 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md @@ -183,6 +183,22 @@ function findTheWinner(n: number, k: number): number { } ``` +#### JavaScript + +```js +function findTheWinner(n, k) { + const arr = Array.from({ length: n }, (_, i) => i + 1); + let i = 0; + + while (arr.length > 1) { + i = (i + k - 1) % arr.length; + arr.splice(i, 1); + } + + return arr[0]; +} +``` + diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md b/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md index 748158d329787..079445a1a3ecd 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md @@ -182,6 +182,22 @@ function findTheWinner(n: number, k: number): number { } ``` +#### JavaScript + +```js +function findTheWinner(n, k) { + const arr = Array.from({ length: n }, (_, i) => i + 1); + let i = 0; + + while (arr.length > 1) { + i = (i + k - 1) % arr.length; + arr.splice(i, 1); + } + + return arr[0]; +} +``` + diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution2.js b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution2.js new file mode 100644 index 0000000000000..77b3d801c9aba --- /dev/null +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution2.js @@ -0,0 +1,11 @@ +function findTheWinner(n, k) { + const arr = Array.from({ length: n }, (_, i) => i + 1); + let i = 0; + + while (arr.length > 1) { + i = (i + k - 1) % arr.length; + arr.splice(i, 1); + } + + return arr[0]; +} From 82937e02d61cf7ba806b18a627fd6a5b6643bb16 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 8 Jul 2024 16:57:11 +0800 Subject: [PATCH 5/8] Update README.md --- .../README.md | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md b/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md index a23d432282e3c..2233b825aaa05 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/README.md @@ -141,20 +141,29 @@ func findTheWinner(n int, k int) int { ```ts function findTheWinner(n: number, k: number): number { - if (n === 1) return 1; - const res = (findTheWinner(n - 1, k) + k) % n; - return res ? res : n; + if (n === 1) { + return 1; + } + const ans = (k + findTheWinner(n - 1, k)) % n; + return ans ? ans : n; } ``` #### JavaScript ```js -function findTheWinner(n, k) { - if (n === 1) return 1; - const res = (findTheWinner(n - 1, k) + k) % n; - return res ? res : n; -} +/** + * @param {number} n + * @param {number} k + * @return {number} + */ +var findTheWinner = function (n, k) { + if (n === 1) { + return 1; + } + const ans = (k + findTheWinner(n - 1, k)) % n; + return ans ? ans : n; +}; ``` @@ -163,7 +172,7 @@ function findTheWinner(n, k) { -### Solution 2. Simulation +### 方法二:模拟 From 9bdd7f68404df854406a7ff28fed937a61a7e5a3 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 8 Jul 2024 16:58:02 +0800 Subject: [PATCH 6/8] Update README_EN.md --- .../README_EN.md | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md b/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md index 079445a1a3ecd..ae70a45fec097 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/README_EN.md @@ -140,20 +140,29 @@ func findTheWinner(n int, k int) int { ```ts function findTheWinner(n: number, k: number): number { - if (n === 1) return 1; - const res = (findTheWinner(n - 1, k) + k) % n; - return res ? res : n; + if (n === 1) { + return 1; + } + const ans = (k + findTheWinner(n - 1, k)) % n; + return ans ? ans : n; } ``` #### JavaScript ```js -function findTheWinner(n, k) { - if (n === 1) return 1; - const res = (findTheWinner(n - 1, k) + k) % n; - return res ? res : n; -} +/** + * @param {number} n + * @param {number} k + * @return {number} + */ +var findTheWinner = function (n, k) { + if (n === 1) { + return 1; + } + const ans = (k + findTheWinner(n - 1, k)) % n; + return ans ? ans : n; +}; ``` @@ -162,7 +171,7 @@ function findTheWinner(n, k) { -### Solution 2. Simulation +### Solution 2: Simulation From ba543a0be86e4e99dd6762290100cbe4cadae3b9 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 8 Jul 2024 16:58:20 +0800 Subject: [PATCH 7/8] Update Solution.js --- .../Solution.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.js b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.js index 63815f88d8317..c0be3fb81487b 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.js +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.js @@ -1,5 +1,12 @@ -function findTheWinner(n, k) { - if (n === 1) return 1; - const res = (findTheWinner(n - 1, k) + k) % n; - return res ? res : n; -} +/** + * @param {number} n + * @param {number} k + * @return {number} + */ +var findTheWinner = function (n, k) { + if (n === 1) { + return 1; + } + const ans = (k + findTheWinner(n - 1, k)) % n; + return ans ? ans : n; +}; From d48ca2cbafe3a64bfc1ca5b8eeaf959743866c14 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 8 Jul 2024 16:58:35 +0800 Subject: [PATCH 8/8] Update Solution.ts --- .../1823.Find the Winner of the Circular Game/Solution.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.ts b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.ts index 8f8e8e3c70b8c..6cf955dca4825 100644 --- a/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.ts +++ b/solution/1800-1899/1823.Find the Winner of the Circular Game/Solution.ts @@ -1,5 +1,7 @@ function findTheWinner(n: number, k: number): number { - if (n === 1) return 1; - const res = (findTheWinner(n - 1, k) + k) % n; - return res ? res : n; + if (n === 1) { + return 1; + } + const ans = (k + findTheWinner(n - 1, k)) % n; + return ans ? ans : n; }