-
Notifications
You must be signed in to change notification settings - Fork 0
/
Group the People Given the Group Size They Belong To.cpp
49 lines (40 loc) · 1.78 KB
/
Group the People Given the Group Size They Belong To.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Problem Title: Group the People Given the Group Size They Belong To
Problem URL: https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/
Description: There are n people that are split into some unknown number of groups.
Each person is labeled with a unique ID from 0 to n - 1.
You are given an integer array groupSizes, where groupSizes[i] is the size of the group that person i is in.
For example, if groupSizes[1] = 3, then person 1 must be in a group of size 3.
Return a list of groups such that each person i is in a group of size groupSizes[i].
Each person should appear in exactly one group, and every person must be in a group.
If there are multiple answers, return any of them.
It is guaranteed that there will be at least one valid solution for the given input.
Difficulty: Medium
Language: C++
Category: Algorithms
*/
class Solution {
public:
vector<vector<int>> groupThePeople(vector<int>& groupSizes)
{
vector<vector<int>>groups;
map<int, vector<int>> mp;
map<int, vector<int>>::iterator it;
for (int i = 0; i < groupSizes.size(); ++i)
mp[groupSizes[i]].push_back(i);
for(it=mp.begin(); it != mp.end(); ++it)
{
int groupSize = it->first, k=0;
vector<int> persons = it->second;
int bulks = persons.size()/groupSize;
while(bulks--)
{
vector<int> newBulk;
for(int i = 0; i < groupSize; ++i)
newBulk.push_back(persons[k++]);
groups.push_back(newBulk);
}
}
return groups;
}
};