1282.cpp (609B)
1 class Solution { 2 public: 3 vector<vector<int>> groupThePeople(const vector<int> &groupSizes) { 4 unordered_map<int, vector<int>> um; 5 vector<vector<int>> res; 6 7 for (int i = 0; i < groupSizes.size(); i++) { 8 if (groupSizes[i] == 1) 9 res.push_back({i}); 10 else { 11 int size = groupSizes[i]; 12 um[size].push_back(i); 13 if (um[size].size() == size) { 14 res.push_back({}); 15 swap(res.back(), um[size]); 16 } 17 } 18 } 19 20 return res; 21 } 22 };