11. Group the People Given the Group Size They Belong To
The problem can be found at the following link: Question Link
My Approach
One way to approach this problem is by using a map data structure from C++ STL library. We can iterate through the input array and store the indices of all the people belonging to a particular group size in a vector. By doing so, we can group all the people based on their group sizes. After that, we can iterate through the map and divide each group into subgroups of size groupSize.
Initialize an empty map mp.
Iterate through the groupSizes array using a loop.
For each element groupSizes[i], insert the index i into the corresponding vector in the map mp
groupSizes[i]
.Iterate through the mp map using a loop.
For each key-value pair in the map, store the key in k and the value in vec.
Initialize a count variable count to 0 and an empty vector temp.
For each index vec[i], push it into the temp vector and increment the count variable.
If the count variable becomes equal to k, push the temp vector into the ans vector, reset the count variable to 0, and clear the temp vector.
Return the ans vector, which contains all the subgroups of people grouped by their respective group sizes.
Time and Auxiliary Space Complexity
Time Complexity:
O(nlogn)
Auxiliary Space Complexity:
O(n)
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the rishabhv12/Daily-Leetcode-Solution repository.
Last updated