forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
37 lines (31 loc) · 826 Bytes
/
solution.cpp
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
class Solution {
public:
int get_gcd(int a, int b) {
int h = max(a, b);
int l = min(a, b);
if (l == 0) return h;
int m = h % l;
while (m) {
h = l;
l = m;
m = h % l;
}
return l;
}
bool hasGroupsSizeX(vector<int>& deck) {
map<int, int> count;
for(auto i:deck){
if(count.find(i) != count.end())
count[i] += 1;
else
count[i] = 1;
}
int min_gcd = count.begin()->second;
int last = min_gcd;
for(auto i=count.begin();i!=count.end();i++){
min_gcd = min(min_gcd, get_gcd(i->second,last));
last = i->second;
}
return min_gcd>=2;
}
};