-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path914.py
More file actions
42 lines (42 loc) · 1.36 KB
/
914.py
File metadata and controls
42 lines (42 loc) · 1.36 KB
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
__________________________________________________________________________________________________
sample 140 ms submission
class Solution:
def hasGroupsSizeX(self, deck: List[int]) -> bool:
deck.sort()
lengths = []
for key, group in itertools.groupby(deck):
temp = len(list(group))
if temp < 2:
return False
lengths.append(temp)
def gcd(a,b):
if b==0:
return a
else:
return gcd(b,a%b)
g = lengths[0]
for j in lengths[1:]:
g = gcd(g,j)
return g >= 2
__________________________________________________________________________________________________
sample 13080 kb submission
class Solution:
def hasGroupsSizeX(self, deck: List[int]) -> bool:
def gcd(x,y):
while x!=y and y!=0:
a = x%y
x,y = y,a
return x
d = collections.defaultdict(int)
for card in deck:
d[card]+=1
g = None
for k in d:
if d[k]==1: return False
if not g: g = d[k]
else:
if d[k]%g==0: continue
g = gcd(g, d[k])
if g<2: return False
return True
__________________________________________________________________________________________________