1282.用户分组
1282. 用户分组
输入:groupSizes = [3,3,3,3,3,1,3] 输出:[[5],[0,1,2],[3,4,6]] 解释: 其他可能的解决方案有 [[2,1,6],[5],[0,4,3]] 和 [[5],[0,6,2],[4,3,1]]。 示例 2:
输入:groupSizes = [2,1,3,3,3,2] 输出:[[1],[0,5],[2,3,4]]groupSizes.length == n 1 <= n <= 500 1 <= groupSizes[i] <= nclass Solution {
public:
vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
vector<vector<int>> res,tmp;
tmp.resize(groupSizes.size());
for(int i=0;i<groupSizes.size();i++)
{
tmp[groupSizes[i]-1].push_back(i);
}
for(int g =0; g<tmp.size();g++)
{
if(!tmp[g].empty())
{
for(int i=0;i<tmp[g].size()/(g+1);i++)
{
vector<int>te;
for(int j=0;j<g+1;j++)
te.push_back(tmp[g][i*(g+1)+j]);
res.push_back(te);
}
}
}
return res;
}
};Last updated