-
Notifications
You must be signed in to change notification settings - Fork 0
/
hands_of_staraights.cpp
26 lines (25 loc) · 973 Bytes
/
hands_of_staraights.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
/*
Alice has some cards, each card has one number written on it. She wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards.
Given an integer array hand of size N where hand [ i ] is the value written on the ith card and an integer groupSize, return true if she can rearrange the cards, or false otherwise.
*/
bool isStraightHand(int N, int groupSize, vector<int> &hand) {
int x=groupSize;
if(N%groupSize) return false;
map<int,int> m;
for(auto&it:hand) m[it]++;
while(!m.empty()){
auto it = m.begin();
int e=it->first;
m[it->first]--;
if(m[it->first]==0) m.erase(it);
int s(1);
while(s<x){
e++;
if(m[e]==0) return false;
m[e]--;
if(m[e]==0) m.erase(e);
s++;
}
}
return true;
}