Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7a8178e

Browse files
authored
feat: add ts solution to lc problem: No.0846 (doocs#3059)
1 parent 451b2e6 commit 7a8178e

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed

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

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

172+
#### TypeScript
173+
174+
```ts
175+
function isNStraightHand(hand: number[], groupSize: number) {
176+
const cnt: Record<number, number> = {};
177+
for (const i of hand) {
178+
cnt[i] = (cnt[i] ?? 0) + 1;
179+
}
180+
181+
const keys = Object.keys(cnt).map(Number);
182+
for (const i of keys) {
183+
while (cnt[i]) {
184+
for (let j = i; j < groupSize + i; j++) {
185+
if (!cnt[j]) {
186+
return false;
187+
}
188+
cnt[j]--;
189+
}
190+
}
191+
}
192+
193+
return true;
194+
}
195+
```
196+
172197
<!-- tabs:end -->
173198

174199
<!-- solution:end -->
@@ -299,6 +324,40 @@ func isNStraightHand(hand []int, groupSize int) bool {
299324
}
300325
```
301326

327+
#### TypeScript
328+
329+
```ts
330+
function isNStraightHand(hand: number[], groupSize: number): boolean {
331+
const n = hand.length;
332+
if (n % groupSize) {
333+
return false;
334+
}
335+
336+
const groups: number[][] = Array.from({ length: n / groupSize }, () => []);
337+
hand.sort((a, b) => a - b);
338+
339+
for (let i = 0; i < n; i++) {
340+
let isPushed = false;
341+
342+
for (const g of groups) {
343+
if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) {
344+
continue;
345+
}
346+
347+
g.push(hand[i]);
348+
isPushed = true;
349+
break;
350+
}
351+
352+
if (!isPushed) {
353+
return false;
354+
}
355+
}
356+
357+
return true;
358+
}
359+
```
360+
302361
<!-- tabs:end -->
303362

304363
<!-- solution:end -->

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

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

163+
#### TypeScript
164+
165+
```ts
166+
function isNStraightHand(hand: number[], groupSize: number) {
167+
const cnt: Record<number, number> = {};
168+
for (const i of hand) {
169+
cnt[i] = (cnt[i] ?? 0) + 1;
170+
}
171+
172+
const keys = Object.keys(cnt).map(Number);
173+
for (const i of keys) {
174+
while (cnt[i]) {
175+
for (let j = i; j < groupSize + i; j++) {
176+
if (!cnt[j]) {
177+
return false;
178+
}
179+
cnt[j]--;
180+
}
181+
}
182+
}
183+
184+
return true;
185+
}
186+
```
187+
163188
<!-- tabs:end -->
164189

165190
<!-- solution:end -->
@@ -284,6 +309,40 @@ func isNStraightHand(hand []int, groupSize int) bool {
284309
}
285310
```
286311

312+
#### TypeScript
313+
314+
```ts
315+
function isNStraightHand(hand: number[], groupSize: number): boolean {
316+
const n = hand.length;
317+
if (n % groupSize) {
318+
return false;
319+
}
320+
321+
const groups: number[][] = Array.from({ length: n / groupSize }, () => []);
322+
hand.sort((a, b) => a - b);
323+
324+
for (let i = 0; i < n; i++) {
325+
let isPushed = false;
326+
327+
for (const g of groups) {
328+
if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) {
329+
continue;
330+
}
331+
332+
g.push(hand[i]);
333+
isPushed = true;
334+
break;
335+
}
336+
337+
if (!isPushed) {
338+
return false;
339+
}
340+
}
341+
342+
return true;
343+
}
344+
```
345+
287346
<!-- tabs:end -->
288347

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /