-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0846.HandOfStraights.cpp
More file actions
40 lines (36 loc) · 997 Bytes
/
0846.HandOfStraights.cpp
File metadata and controls
40 lines (36 loc) · 997 Bytes
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
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int groupSize) {
// Speed thingies.
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// Get sorted card counts.
map<int, int> cardCounts;
for (int i = 0; i < hand.size(); i++) {
auto it = cardCounts.find(hand[i]);
if (it == cardCounts.end()) {
cardCounts.emplace(hand[i], 1);
} else {
it->second++;
}
}
vector<map<int, int>::iterator> cards(groupSize);
while (!cardCounts.empty()) {
// Get cards in group.
cards[0] = cardCounts.begin();
for (int i = 1; i < groupSize; i++) {
// Find next card in group.
cards[i] = cardCounts.find(cards[i - 1]->first + 1);
// Check if card valid.
if (cards[i] == cardCounts.end()) return false;
}
// Update card count and remove if no more remain.
for (int i = 0; i < groupSize; i++) {
cards[i]->second--;
if (cards[i]->second <= 0) cardCounts.erase(cards[i]);
}
}
return true;
}
};