-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhand-of-straights.py
77 lines (70 loc) · 2.71 KB
/
hand-of-straights.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 846. Hand of Straights
# 🟠 Medium
#
# https://leetcode.com/problems/hand-of-straights/
#
# Tags: Array - Hash Table - Greedy - Sorting
import timeit
from collections import Counter
from typing import List
# Note: This question is the same as Leetcode 1296 Divide Array in Sets
# of K Consecutive Numbers.
# This problem is also similar to LeetCode 659. Split Array into
# Consecutive Subsequences, we can try a similar approach using a
# frequencies dictionary and greedily trying to build the sequences
# of length groupSize.
#
# Time complexity: O(n*log(n)) - Sorting has the biggest complexity,
# then we visit each card a maximum of two times for O(n).
# Space complexity: O(n) - The frequencies dictionary.
#
# Runtime: 210 ms, faster than 95.48%
# Memory Usage: 15.7 MB, less than 75.38%
class Solution:
def isNStraightHand(self, hand: List[int], groupSize: int) -> bool:
# Sorting the input we make sure we visit smaller values first.
hand.sort()
# Store the frequencies of the elements we have available.
available = Counter(hand)
# Iterate over the sorted cards and try to build a sequence of
# length group size.
for card in hand:
# Check if we have used all the cards of that value already.
if available[card] <= 0:
continue
# We have, at least, one card of this value available, try
# to build a sequence starting at this card.
for i in range(groupSize):
val = card + i
# If we are missing any of the cards to build this
# sequence, we have failed already.
if available[val] <= 0:
return False
# Register the fact that we are using one card of this
# value.
available[val] -= 1
# If we manage to place all cards in sequences, we succeeded.
return True
def test():
executors = [Solution]
tests = [
[[1, 2, 3, 6, 2, 3, 4, 7, 8], 3, True],
[[1, 2, 3, 4, 5], 4, False],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for n, t in enumerate(tests):
sol = executor()
result = sol.isNStraightHand(t[0], t[1])
exp = t[2]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for "
+ f"test {n} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()