File tree Expand file tree Collapse file tree 4 files changed +167
-0
lines changed
solution/0800-0899/0846.Hand of Straights Expand file tree Collapse file tree 4 files changed +167
-0
lines changed Original file line number Diff line number Diff line change @@ -169,6 +169,31 @@ func isNStraightHand(hand []int, groupSize int) bool {
169
169
}
170
170
```
171
171
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
+
172
197
<!-- tabs: end -->
173
198
174
199
<!-- solution: end -->
@@ -299,6 +324,40 @@ func isNStraightHand(hand []int, groupSize int) bool {
299
324
}
300
325
```
301
326
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
+
302
361
<!-- tabs: end -->
303
362
304
363
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -160,6 +160,31 @@ func isNStraightHand(hand []int, groupSize int) bool {
160
160
}
161
161
```
162
162
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
+
163
188
<!-- tabs: end -->
164
189
165
190
<!-- solution: end -->
@@ -284,6 +309,40 @@ func isNStraightHand(hand []int, groupSize int) bool {
284
309
}
285
310
```
286
311
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
+
287
346
<!-- tabs: end -->
288
347
289
348
<!-- solution: end -->
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments