Hand of Straights LeetCode solution in C++

1
class Solution {
2
public:
3
bool isNStraightHand(vector<int> &hand, int groupSize) {
4
int n = hand.size();
5
if (n % groupSize != 0) // We can't rearrange elements of size groupsize so return false
6
return false;
7
map<int, int> mp;
8
for (auto it : hand) mp[it]++;
9
while (mp.size() > 0) {
10
int start = mp.begin()->first; // Taking the topmost element of map
11

12
for (int i = 0; i < groupSize; i++) {
13
if (mp.find(start) != mp.end()) {
14
mp[start]--;
15
if (mp[start] == 0) mp.erase(start);
16
start++; // Increasing for finding next consecutive element of start.
17
} else
18
return false;
19
}
20
}
21
return true;
22
}
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0