diff --git a/solution/0800-0899/0846.Hand of Straights/README.md b/solution/0800-0899/0846.Hand of Straights/README.md index 95346c2cf10b4..86dacf4f968e7 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 +function isNStraightHand(hand: number[], groupSize: number) { + const cnt: Record = {}; + for (const i of hand) { + cnt[i] = (cnt[i] ?? 0) + 1; + } + + const keys = Object.keys(cnt).map(Number); + for (const i of keys) { + while (cnt[i]) { + for (let j = i; j < groupSize + i; j++) { + if (!cnt[j]) { + return false; + } + cnt[j]--; + } + } + } + + return true; +} +``` + @@ -299,6 +324,40 @@ 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..97943c10c5a70 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 +function isNStraightHand(hand: number[], groupSize: number) { + const cnt: Record = {}; + for (const i of hand) { + cnt[i] = (cnt[i] ?? 0) + 1; + } + + const keys = Object.keys(cnt).map(Number); + for (const i of keys) { + while (cnt[i]) { + for (let j = i; j < groupSize + i; j++) { + if (!cnt[j]) { + return false; + } + cnt[j]--; + } + } + } + + return true; +} +``` + @@ -284,6 +309,40 @@ 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..0396d0bf50443 --- /dev/null +++ b/solution/0800-0899/0846.Hand of Straights/Solution.ts @@ -0,0 +1,20 @@ +function isNStraightHand(hand: number[], groupSize: number) { + const cnt: Record = {}; + for (const i of hand) { + cnt[i] = (cnt[i] ?? 0) + 1; + } + + const keys = Object.keys(cnt).map(Number); + for (const i of keys) { + while (cnt[i]) { + for (let j = i; j < groupSize + i; j++) { + if (!cnt[j]) { + return false; + } + cnt[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..5cd2d00b224b8 --- /dev/null +++ b/solution/0800-0899/0846.Hand of Straights/Solution2.ts @@ -0,0 +1,29 @@ +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; +}