-
Notifications
You must be signed in to change notification settings - Fork 43
/
x-of-a-kind-in-a-deck-of-cards.py
44 lines (40 loc) · 1.05 KB
/
x-of-a-kind-in-a-deck-of-cards.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
# V1 : dev
# V2
# https://blog.csdn.net/danspace1/article/details/88956805
class Solution(object):
def hasGroupsSizeX(self, deck):
"""
:type deck: List[int]
:rtype: bool
"""
def check(i):
for n in d:
if d[n] % i != 0:
return False
return True
if len(deck) < 2:
return False
d = collections.Counter(deck)
m = min(d.values())
for i in range(2, m+1):
# check if the great common divisor is i
if check(i):
return True
return False
# V3
# Time: O(n * (logn)^2)
# Space: O(n)
import collections
from functools import reduce
class Solution(object):
def hasGroupsSizeX(self, deck):
"""
:type deck: List[int]
:rtype: bool
"""
def gcd(a, b): # Time: O((logn)^2)
while b:
a, b = b, a % b
return a
vals = list(collections.Counter(deck).values())
return reduce(gcd, vals) >= 2