Skip to content

Commit 9d59647

Browse files
authored
Merge pull request doocs#242 from phuclhv/master
Add Python Solution to 0914
2 parents 2251ab9 + ac6682b commit 9d59647

File tree

1 file changed

+43
-0
lines changed
  • solution/0914.X of a Kind in a Deck of Cards

1 file changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Algorithm
2+
'''
3+
1) Use a hashmap to store occurences
4+
2) Find the min value in that hashmap, then find all of its factor
5+
3) Return true when every occurences share that factor
6+
7+
'''
8+
# Performance
9+
'''
10+
Runtime: 144 ms, faster than 87.35% of Python3 online submissions for X of a Kind in a Deck of Cards.
11+
Memory Usage: 12.9 MB, less than 100.00% of Python3 online submissions for X of a Kind in a Deck of Cards.
12+
'''
13+
14+
from collections import defaultdict
15+
class Solution:
16+
def hasGroupsSizeX(self, deck: List[int]) -> bool:
17+
18+
if len(deck) < 2:
19+
return False
20+
21+
occurences = {}
22+
for card in deck:
23+
if card not in occurences:
24+
occurences[card] = 1
25+
else:
26+
occurences[card] += 1
27+
28+
min_occurence = occurences[min(occurences.keys(), key = (lambda k: occurences[k]))]
29+
30+
for factor in range(2, min_occurence+1):
31+
if min_occurence % factor == 0:
32+
check = True
33+
for value in occurences.values():
34+
if value % factor != 0:
35+
check = False
36+
break
37+
if check:
38+
return True
39+
40+
return False
41+
42+
43+

0 commit comments

Comments
 (0)