From 6e804a31c700033c4b651751a5916d8590ca54b8 Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 6 Jun 2024 19:19:16 +0300 Subject: [PATCH 1/7] feat: feat: add ts solution to lc problem: No.0846 --- .../0846.Hand of Straights/README.md | 53 +++++++++++++++++++ .../0846.Hand of Straights/README_EN.md | 53 +++++++++++++++++++ .../0846.Hand of Straights/Solution.ts | 20 +++++++ .../0846.Hand of Straights/Solution2.ts | 23 ++++++++ 4 files changed, 149 insertions(+) create mode 100644 solution/0800-0899/0846.Hand of Straights/Solution.ts create mode 100644 solution/0800-0899/0846.Hand of Straights/Solution2.ts diff --git a/solution/0800-0899/0846.Hand of Straights/README.md b/solution/0800-0899/0846.Hand of Straights/README.md index 95346c2cf10b4..08ad022def3ac 100644 --- a/solution/0800-0899/0846.Hand of Straights/README.md +++ b/solution/0800-0899/0846.Hand of Straights/README.md @@ -169,6 +169,31 @@ func isNStraightHand(hand []int, groupSize int) bool { } ``` +#### TypeScript + +```ts +export function isNStraightHand(hand: number[], groupSize: number) { + const map: Record = {}; + + for (const i of hand) { + map[i] = (map[i] ?? 0) + 1; + } + + const keys = Object.keys(map).map(Number); + + for (const i of keys) { + while (map[i]) { + for (let j = i; j < groupSize + i; j++) { + if (!map[j]) return false; + map[j]--; + } + } + } + + return true; +} +``` + @@ -299,6 +324,34 @@ func isNStraightHand(hand []int, groupSize int) bool { } ``` +#### TypeScript + +```ts +function isNStraightHand(hand: number[], groupSize: number): boolean { + const n = hand.length; + if (n % groupSize) return false; + + const groups: number[][] = Array.from({ length: n / groupSize }, () => []); + hand.sort((a, b) => a - b); + + for (let i = 0; i < n; i++) { + let isPushed = false; + + for (const g of groups) { + if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) continue; + + g.push(hand[i]); + isPushed = true; + break; + } + + if (!isPushed) return false; + } + + return true; +} +``` + diff --git a/solution/0800-0899/0846.Hand of Straights/README_EN.md b/solution/0800-0899/0846.Hand of Straights/README_EN.md index cf05422e5303a..a6054b6c35c18 100644 --- a/solution/0800-0899/0846.Hand of Straights/README_EN.md +++ b/solution/0800-0899/0846.Hand of Straights/README_EN.md @@ -160,6 +160,31 @@ func isNStraightHand(hand []int, groupSize int) bool { } ``` +#### TypeScript + +```ts +export function isNStraightHand(hand: number[], groupSize: number) { + const map: Record = {}; + + for (const i of hand) { + map[i] = (map[i] ?? 0) + 1; + } + + const keys = Object.keys(map).map(Number); + + for (const i of keys) { + while (map[i]) { + for (let j = i; j < groupSize + i; j++) { + if (!map[j]) return false; + map[j]--; + } + } + } + + return true; +} +``` + @@ -284,6 +309,34 @@ func isNStraightHand(hand []int, groupSize int) bool { } ``` +#### TypeScript + +```ts +function isNStraightHand(hand: number[], groupSize: number): boolean { + const n = hand.length; + if (n % groupSize) return false; + + const groups: number[][] = Array.from({ length: n / groupSize }, () => []); + hand.sort((a, b) => a - b); + + for (let i = 0; i < n; i++) { + let isPushed = false; + + for (const g of groups) { + if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) continue; + + g.push(hand[i]); + isPushed = true; + break; + } + + if (!isPushed) return false; + } + + return true; +} +``` + diff --git a/solution/0800-0899/0846.Hand of Straights/Solution.ts b/solution/0800-0899/0846.Hand of Straights/Solution.ts new file mode 100644 index 0000000000000..f41c56133063b --- /dev/null +++ b/solution/0800-0899/0846.Hand of Straights/Solution.ts @@ -0,0 +1,20 @@ +export function isNStraightHand(hand: number[], groupSize: number) { + const map: Record = {}; + + for (const i of hand) { + map[i] = (map[i] ?? 0) + 1; + } + + const keys = Object.keys(map).map(Number); + + for (const i of keys) { + while (map[i]) { + for (let j = i; j < groupSize + i; j++) { + if (!map[j]) return false; + map[j]--; + } + } + } + + return true; +} diff --git a/solution/0800-0899/0846.Hand of Straights/Solution2.ts b/solution/0800-0899/0846.Hand of Straights/Solution2.ts new file mode 100644 index 0000000000000..e6beac797eb7d --- /dev/null +++ b/solution/0800-0899/0846.Hand of Straights/Solution2.ts @@ -0,0 +1,23 @@ +function isNStraightHand(hand: number[], groupSize: number): boolean { + const n = hand.length; + if (n % groupSize) return false; + + const groups: number[][] = Array.from({ length: n / groupSize }, () => []); + hand.sort((a, b) => a - b); + + for (let i = 0; i < n; i++) { + let isPushed = false; + + for (const g of groups) { + if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) continue; + + g.push(hand[i]); + isPushed = true; + break; + } + + if (!isPushed) return false; + } + + return true; +} From b93d0f56c85e2c4e2f344ac5a959ed25179e9f89 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 7 Jun 2024 17:32:45 +0800 Subject: [PATCH 2/7] Update Solution.ts --- .../0846.Hand of Straights/Solution.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/solution/0800-0899/0846.Hand of Straights/Solution.ts b/solution/0800-0899/0846.Hand of Straights/Solution.ts index f41c56133063b..0396d0bf50443 100644 --- a/solution/0800-0899/0846.Hand of Straights/Solution.ts +++ b/solution/0800-0899/0846.Hand of Straights/Solution.ts @@ -1,17 +1,17 @@ -export function isNStraightHand(hand: number[], groupSize: number) { - const map: Record = {}; - +function isNStraightHand(hand: number[], groupSize: number) { + const cnt: Record = {}; for (const i of hand) { - map[i] = (map[i] ?? 0) + 1; + cnt[i] = (cnt[i] ?? 0) + 1; } - const keys = Object.keys(map).map(Number); - + const keys = Object.keys(cnt).map(Number); for (const i of keys) { - while (map[i]) { + while (cnt[i]) { for (let j = i; j < groupSize + i; j++) { - if (!map[j]) return false; - map[j]--; + if (!cnt[j]) { + return false; + } + cnt[j]--; } } } From 70614c5368fa15e4b292988a0ea34abf460a3d1b Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 7 Jun 2024 17:33:14 +0800 Subject: [PATCH 3/7] Update README.md --- .../0800-0899/0846.Hand of Straights/README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/solution/0800-0899/0846.Hand of Straights/README.md b/solution/0800-0899/0846.Hand of Straights/README.md index 08ad022def3ac..5c34bf74885f3 100644 --- a/solution/0800-0899/0846.Hand of Straights/README.md +++ b/solution/0800-0899/0846.Hand of Straights/README.md @@ -172,20 +172,20 @@ func isNStraightHand(hand []int, groupSize int) bool { #### TypeScript ```ts -export function isNStraightHand(hand: number[], groupSize: number) { - const map: Record = {}; - +function isNStraightHand(hand: number[], groupSize: number) { + const cnt: Record = {}; for (const i of hand) { - map[i] = (map[i] ?? 0) + 1; + cnt[i] = (cnt[i] ?? 0) + 1; } - const keys = Object.keys(map).map(Number); - + const keys = Object.keys(cnt).map(Number); for (const i of keys) { - while (map[i]) { + while (cnt[i]) { for (let j = i; j < groupSize + i; j++) { - if (!map[j]) return false; - map[j]--; + if (!cnt[j]) { + return false; + } + cnt[j]--; } } } From 4e56d96a8843f7ba219e1fbd79cabdc07cc7a6d0 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 7 Jun 2024 17:33:29 +0800 Subject: [PATCH 4/7] Update README_EN.md --- .../0846.Hand of Straights/README_EN.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/solution/0800-0899/0846.Hand of Straights/README_EN.md b/solution/0800-0899/0846.Hand of Straights/README_EN.md index a6054b6c35c18..d8f3de7d2b0b4 100644 --- a/solution/0800-0899/0846.Hand of Straights/README_EN.md +++ b/solution/0800-0899/0846.Hand of Straights/README_EN.md @@ -163,20 +163,20 @@ func isNStraightHand(hand []int, groupSize int) bool { #### TypeScript ```ts -export function isNStraightHand(hand: number[], groupSize: number) { - const map: Record = {}; - +function isNStraightHand(hand: number[], groupSize: number) { + const cnt: Record = {}; for (const i of hand) { - map[i] = (map[i] ?? 0) + 1; + cnt[i] = (cnt[i] ?? 0) + 1; } - const keys = Object.keys(map).map(Number); - + const keys = Object.keys(cnt).map(Number); for (const i of keys) { - while (map[i]) { + while (cnt[i]) { for (let j = i; j < groupSize + i; j++) { - if (!map[j]) return false; - map[j]--; + if (!cnt[j]) { + return false; + } + cnt[j]--; } } } From c8025fa7886b0630985d4dcc520340dbb2f08d7c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 7 Jun 2024 17:34:32 +0800 Subject: [PATCH 5/7] Update Solution2.ts --- .../0800-0899/0846.Hand of Straights/Solution2.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/solution/0800-0899/0846.Hand of Straights/Solution2.ts b/solution/0800-0899/0846.Hand of Straights/Solution2.ts index e6beac797eb7d..5cd2d00b224b8 100644 --- a/solution/0800-0899/0846.Hand of Straights/Solution2.ts +++ b/solution/0800-0899/0846.Hand of Straights/Solution2.ts @@ -1,6 +1,8 @@ function isNStraightHand(hand: number[], groupSize: number): boolean { const n = hand.length; - if (n % groupSize) return false; + if (n % groupSize) { + return false; + } const groups: number[][] = Array.from({ length: n / groupSize }, () => []); hand.sort((a, b) => a - b); @@ -9,14 +11,18 @@ function isNStraightHand(hand: number[], groupSize: number): boolean { let isPushed = false; for (const g of groups) { - if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) continue; + if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) { + continue; + } g.push(hand[i]); isPushed = true; break; } - if (!isPushed) return false; + if (!isPushed) { + return false; + } } return true; From 20cdefc2efc7703e551a0b77fffbe8495c7cd51d Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 7 Jun 2024 17:34:49 +0800 Subject: [PATCH 6/7] Update README.md --- solution/0800-0899/0846.Hand of Straights/README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/solution/0800-0899/0846.Hand of Straights/README.md b/solution/0800-0899/0846.Hand of Straights/README.md index 5c34bf74885f3..86dacf4f968e7 100644 --- a/solution/0800-0899/0846.Hand of Straights/README.md +++ b/solution/0800-0899/0846.Hand of Straights/README.md @@ -329,7 +329,9 @@ func isNStraightHand(hand []int, groupSize int) bool { ```ts function isNStraightHand(hand: number[], groupSize: number): boolean { const n = hand.length; - if (n % groupSize) return false; + if (n % groupSize) { + return false; + } const groups: number[][] = Array.from({ length: n / groupSize }, () => []); hand.sort((a, b) => a - b); @@ -338,14 +340,18 @@ function isNStraightHand(hand: number[], groupSize: number): boolean { let isPushed = false; for (const g of groups) { - if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) continue; + if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) { + continue; + } g.push(hand[i]); isPushed = true; break; } - if (!isPushed) return false; + if (!isPushed) { + return false; + } } return true; From d9b24b59fa1cd27ff166ef67cabf213cf880aa16 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 7 Jun 2024 17:35:00 +0800 Subject: [PATCH 7/7] Update README_EN.md --- .../0800-0899/0846.Hand of Straights/README_EN.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/solution/0800-0899/0846.Hand of Straights/README_EN.md b/solution/0800-0899/0846.Hand of Straights/README_EN.md index d8f3de7d2b0b4..97943c10c5a70 100644 --- a/solution/0800-0899/0846.Hand of Straights/README_EN.md +++ b/solution/0800-0899/0846.Hand of Straights/README_EN.md @@ -314,7 +314,9 @@ func isNStraightHand(hand []int, groupSize int) bool { ```ts function isNStraightHand(hand: number[], groupSize: number): boolean { const n = hand.length; - if (n % groupSize) return false; + if (n % groupSize) { + return false; + } const groups: number[][] = Array.from({ length: n / groupSize }, () => []); hand.sort((a, b) => a - b); @@ -323,14 +325,18 @@ function isNStraightHand(hand: number[], groupSize: number): boolean { let isPushed = false; for (const g of groups) { - if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) continue; + if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) { + continue; + } g.push(hand[i]); isPushed = true; break; } - if (!isPushed) return false; + if (!isPushed) { + return false; + } } return true;