n
个人(分别被标记为 0
到 n-1
),他们被分到未知数量的组中。
groupSizes
,其中 groupSizes[i]
是第 i
个人所在的组的大小。groupSizes = [3,3,3,3,3,1,3]
。[[5],[0,1,2],[3,4,6]]
。1
的索引存放在同一堆中,后续再在这一堆中不断取索引并生成大小为 1
的组。class Solution
{
public:
vector<vector<int>> groupThePeople(vector<int>& groupSizes)
{
int m = groupSizes.size();
unordered_map<int, vector<int>> mapped;
for(int i = 0; i < m; ++i)
mapped[groupSizes[i]].push_back(i);
vector<vector<int>> ans;
for(auto& [cnt, vec] : mapped)
{
int i = 0;
while(i < vec.size())
{
vector<int> tmp;
for(int j = 0; j < cnt; ++j)
tmp.push_back(vec[i++]);
ans.push_back(tmp);
}
}
return ans;
}
};
class Solution {
public:
vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
unordered_map<int,vector<int>> group;
vector<vector<int>> res;
for(int i = 0;i < groupSizes.size(); ++i){
group[groupSizes[i]].push_back(i);
}
for(auto & it : group){
for(auto curr = it.second.begin(); curr != it.second.end(); curr += it.first){
res.push_back(vector<int>(curr,curr+it.first));
}
}
return res;
}
};