Skip to content

Commit 03746ff

Browse files
committed
feat: feat: add ts solution to lc problem: No.0846
1 parent 1dc1959 commit 03746ff

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

solution/0800-0899/0846.Hand of Straights/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,34 @@ func isNStraightHand(hand []int, groupSize int) bool {
169169
}
170170
```
171171

172+
#### TypeScript
173+
174+
```ts
175+
function isNStraightHand(hand: number[], groupSize: number): boolean {
176+
const n = hand.length;
177+
if (n % groupSize) return false;
178+
179+
const groups: number[][] = Array.from({ length: n / groupSize }, () => []);
180+
hand.sort((a, b) => a - b);
181+
182+
for (let i = 0; i < n; i++) {
183+
let isPushed = false;
184+
185+
for (const g of groups) {
186+
if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) continue;
187+
188+
g.push(hand[i]);
189+
isPushed = true;
190+
break;
191+
}
192+
193+
if (!isPushed) return false;
194+
}
195+
196+
return true;
197+
}
198+
```
199+
172200
<!-- tabs:end -->
173201

174202
<!-- solution:end -->

solution/0800-0899/0846.Hand of Straights/README_EN.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,34 @@ func isNStraightHand(hand []int, groupSize int) bool {
160160
}
161161
```
162162

163+
#### TypeScript
164+
165+
```ts
166+
function isNStraightHand(hand: number[], groupSize: number): boolean {
167+
const n = hand.length;
168+
if (n % groupSize) return false;
169+
170+
const groups: number[][] = Array.from({ length: n / groupSize }, () => []);
171+
hand.sort((a, b) => a - b);
172+
173+
for (let i = 0; i < n; i++) {
174+
let isPushed = false;
175+
176+
for (const g of groups) {
177+
if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) continue;
178+
179+
g.push(hand[i]);
180+
isPushed = true;
181+
break;
182+
}
183+
184+
if (!isPushed) return false;
185+
}
186+
187+
return true;
188+
}
189+
```
190+
163191
<!-- tabs:end -->
164192

165193
<!-- solution:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function isNStraightHand(hand: number[], groupSize: number): boolean {
2+
const n = hand.length;
3+
if (n % groupSize) return false;
4+
5+
const groups: number[][] = Array.from({ length: n / groupSize }, () => []);
6+
hand.sort((a, b) => a - b);
7+
8+
for (let i = 0; i < n; i++) {
9+
let isPushed = false;
10+
11+
for (const g of groups) {
12+
if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) continue;
13+
14+
g.push(hand[i]);
15+
isPushed = true;
16+
break;
17+
}
18+
19+
if (!isPushed) return false;
20+
}
21+
22+
return true;
23+
}

0 commit comments

Comments
 (0)