1
class Solution {
2
public:
3
bool hasGroupsSizeX(vector<int> &deck) {
4
std::unordered_map<int, int> map;
5
for (int i = 0; i < deck.size();
6
i++) // store in unordered_map the amount of cards with each number
7
map[deck[i]]++;
8

9
int x = INT_MAX;
10
for (std::pair<int, int> num : map) // find minimum
11
{
12
if (num.second < x) x = num.second;
13
}
14
if (x < 2) return false;
15

16
for (int i = 2; i <= x; i++) // loop through all numbers smaller than minimum
17
{
18
bool good = true;
19
for (std::pair<int, int> num : map) // if all groups of cards divide by i - flag stays true
20
{
21
if (num.second % i != 0) {
22
good = false;
23
break;
24
}
25
}
26
if (good) return true;
27
}
28

29
return false;
30
}
31
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0