File tree 2 files changed +44
-0
lines changed
2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change 89
89
824|[ Goat Latin] ( ./0824-goat-latin.js ) |Easy|
90
90
831|[ Masking Personal Information] ( ./0831-masking-personal-information.js ) |Medium|
91
91
844|[ Backspace String Compare] ( ./0844-backspace-string-compare.js ) |Easy|
92
+ 846|[ Hand of Straights] ( ./0846-hand-of-straights.js ) |Medium|
92
93
867|[ Transpose Matrix] ( ./0867-transpose-matrix.js ) |Easy|
93
94
884|[ Uncommon Words from Two Sentences] ( ./0884-uncommon-words-from-two-sentences.js ) |Easy|
94
95
890|[ Find and Replace Pattern] ( ./0890-find-and-replace-pattern.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 846. Hand of Straights
3
+ * https://leetcode.com/problems/hand-of-straights/
4
+ * Difficulty: Medium
5
+ *
6
+ * Alice has some number of cards and she wants to rearrange
7
+ * the cards into groups so that each group is of size groupSize,
8
+ * and consists of groupSize consecutive cards.
9
+ *
10
+ * Given an integer array hand where hand[i] is the value written
11
+ * on the ith card and an integer groupSize, return true if she
12
+ * can rearrange the cards, or false otherwise.
13
+ */
14
+
15
+ /**
16
+ * @param {number[] } hand
17
+ * @param {number } groupSize
18
+ * @return {boolean }
19
+ */
20
+ var isNStraightHand = function ( hand , groupSize ) {
21
+ if ( hand . length % groupSize ) {
22
+ return false ;
23
+ }
24
+
25
+ const map = { } ;
26
+ const set = new Set ( hand ) ;
27
+ hand . forEach ( x => map [ x ] ? map [ x ] ++ : map [ x ] = 1 ) ;
28
+
29
+ let count = hand . length / groupSize ;
30
+ while ( count -- ) {
31
+ let min = Math . min ( ...set ) ;
32
+ for ( let i = min ; i < min + groupSize ; i ++ ) {
33
+ if ( ! map [ i ] ) {
34
+ return false ;
35
+ }
36
+ if ( ! -- map [ i ] ) {
37
+ set . delete ( i ) ;
38
+ }
39
+ }
40
+ }
41
+
42
+ return true ;
43
+ } ;
You can’t perform that action at this time.
0 commit comments