File tree 1 file changed +9
-7
lines changed
scripts/algorithms/H/Hand of Straights
1 file changed +9
-7
lines changed Original file line number Diff line number Diff line change
1
+ # Runtime: 304 ms (Top 61.38%) | Memory: 15.7 MB (Top 75.38%)
2
+
1
3
#####################################################################################################################
2
4
# Problem: Hand of Straights
3
5
# Solution : Hash Table, Min Heap
@@ -9,24 +11,24 @@ class Solution:
9
11
def isNStraightHand (self , hand : List [int ], groupSize : int ) -> bool :
10
12
if len (hand ) % groupSize :
11
13
return False
12
-
14
+
13
15
freq = collections .defaultdict (int )
14
-
16
+
15
17
for num in hand :
16
18
freq [num ] += 1
17
-
19
+
18
20
min_heap = list (freq .keys ())
19
21
heapq .heapify (min_heap )
20
-
22
+
21
23
while min_heap :
22
24
smallest = min_heap [0 ]
23
25
for num in range (smallest , smallest + groupSize ):
24
26
if num not in freq :
25
27
return False
26
28
freq [num ] -= 1
27
-
29
+
28
30
if freq [num ] == 0 :
29
31
if num != min_heap [0 ]:
30
32
return False
31
- heapq .heappop (min_heap )
32
- return True
33
+ heapq .heappop (min_heap )
34
+ return True
You can’t perform that action at this time.
0 commit comments