-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandOfStraights.cpp
39 lines (31 loc) · 938 Bytes
/
HandOfStraights.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
38
39
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int groupSize) {
if (hand.size() % groupSize != 0) {
return false;
}
unordered_map<int, int> freq;
for (int i = 0; i < hand.size(); i++) {
freq[hand[i]]++;
}
for (int i = 0; i < hand.size(); i++) {
int start = hand[i];
while (freq[start - 1] != 0) {
start--;
}
while (start <= hand[i]) {
int count = freq[start];
if (count != 0) {
for (int i = 0; i < groupSize; i++) {
if (freq[start + i] < count) {
return false;
}
freq[start + i] -= count;
}
}
start++;
}
}
return true;
}
};