File tree Expand file tree Collapse file tree 4 files changed +149
-0
lines changed
solution/0800-0899/0846.Hand of Straights Expand file tree Collapse file tree 4 files changed +149
-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
+ export function isNStraightHand(hand : number [], groupSize : number ) {
176
+ const map: Record <number , number > = {};
177
+
178
+ for (const i of hand ) {
179
+ map [i ] = (map [i ] ?? 0 ) + 1 ;
180
+ }
181
+
182
+ const keys = Object .keys (map ).map (Number );
183
+
184
+ for (const i of keys ) {
185
+ while (map [i ]) {
186
+ for (let j = i ; j < groupSize + i ; j ++ ) {
187
+ if (! map [j ]) return false ;
188
+ map [j ]-- ;
189
+ }
190
+ }
191
+ }
192
+
193
+ return true ;
194
+ }
195
+ ```
196
+
172
197
<!-- tabs: end -->
173
198
174
199
<!-- solution: end -->
@@ -299,6 +324,34 @@ 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 ) return false ;
333
+
334
+ const groups: number [][] = Array .from ({ length: n / groupSize }, () => []);
335
+ hand .sort ((a , b ) => a - b );
336
+
337
+ for (let i = 0 ; i < n ; i ++ ) {
338
+ let isPushed = false ;
339
+
340
+ for (const g of groups ) {
341
+ if (g .length === groupSize || (g .length && hand [i ] - g .at (- 1 )! !== 1 )) continue ;
342
+
343
+ g .push (hand [i ]);
344
+ isPushed = true ;
345
+ break ;
346
+ }
347
+
348
+ if (! isPushed ) return false ;
349
+ }
350
+
351
+ return true ;
352
+ }
353
+ ```
354
+
302
355
<!-- tabs: end -->
303
356
304
357
<!-- 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
+ export function isNStraightHand(hand : number [], groupSize : number ) {
167
+ const map: Record <number , number > = {};
168
+
169
+ for (const i of hand ) {
170
+ map [i ] = (map [i ] ?? 0 ) + 1 ;
171
+ }
172
+
173
+ const keys = Object .keys (map ).map (Number );
174
+
175
+ for (const i of keys ) {
176
+ while (map [i ]) {
177
+ for (let j = i ; j < groupSize + i ; j ++ ) {
178
+ if (! map [j ]) return false ;
179
+ map [j ]-- ;
180
+ }
181
+ }
182
+ }
183
+
184
+ return true ;
185
+ }
186
+ ```
187
+
163
188
<!-- tabs: end -->
164
189
165
190
<!-- solution: end -->
@@ -284,6 +309,34 @@ 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 ) return false ;
318
+
319
+ const groups: number [][] = Array .from ({ length: n / groupSize }, () => []);
320
+ hand .sort ((a , b ) => a - b );
321
+
322
+ for (let i = 0 ; i < n ; i ++ ) {
323
+ let isPushed = false ;
324
+
325
+ for (const g of groups ) {
326
+ if (g .length === groupSize || (g .length && hand [i ] - g .at (- 1 )! !== 1 )) continue ;
327
+
328
+ g .push (hand [i ]);
329
+ isPushed = true ;
330
+ break ;
331
+ }
332
+
333
+ if (! isPushed ) return false ;
334
+ }
335
+
336
+ return true ;
337
+ }
338
+ ```
339
+
287
340
<!-- tabs: end -->
288
341
289
342
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ export function isNStraightHand ( hand : number [ ] , groupSize : number ) {
2
+ const map : Record < number , number > = { } ;
3
+
4
+ for ( const i of hand ) {
5
+ map [ i ] = ( map [ i ] ?? 0 ) + 1 ;
6
+ }
7
+
8
+ const keys = Object . keys ( map ) . map ( Number ) ;
9
+
10
+ for ( const i of keys ) {
11
+ while ( map [ i ] ) {
12
+ for ( let j = i ; j < groupSize + i ; j ++ ) {
13
+ if ( ! map [ j ] ) return false ;
14
+ map [ 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 ) 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
+ }
You can’t perform that action at this time.
0 commit comments