Group the People Given the Group Size They Belong To LeetCode solution in C++

1
class Solution {
2
public:
3
vector<vector<int>> groupThePeople(vector<int> &groupSizes) {
4
vector<vector<int>> ans;
5
unordered_map<int, vector<int>> store;
6

7
for (int i = 0; i < groupSizes.size(); i++) {
8
if (store[groupSizes[i]].size() == groupSizes[i]) {
9
ans.push_back(store[groupSizes[i]]);
10
store[groupSizes[i]].clear();
11
}
12
store[groupSizes[i]].push_back(i);
13
}
14

15
for (auto &x : store) {
16
ans.push_back(x.second);
17
}
18
return ans;
19
}
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0