Leetcode 1282. Group the People Given the Group Size They Belong To
Group the People Given the Group Size They Belong To - LeetCode
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 people that are split into some unknown number of groups. Each person is labeled with a unique ID from
n - 1.
You are given an integer array
groupSizes[i] is the size of the group that person
i is in. For example, if
groupSizes = 3, then person
1 must be in a group of size
Return a list of groups such that each person
i is in a group of size
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.
Input: groupSizes = [3,3,3,3,3,1,3]
The first group is . The size is 1, and groupSizes = 1.
The second group is [0,1,2]. The size is 3, and groupSizes = groupSizes = groupSizes = 3.
The third group is [3,4,6]. The size is 3, and groupSizes = groupSizes = groupSizes = 3.
Other possible solutions are [[2,1,6],,[0,4,3]] and [,[0,6,2],[4,3,1]].
Input: groupSizes = [2,1,3,3,3,2]
groupSizes.length == n
1 <= n <= 500
1 <= groupSizes[i] <= n
- create a map, and put same group size people into the list
- if list size match the group size number, add it into the result list, and remove the list from map
- In this answer, it had at least one answer, so no need to worry if anyone didn’t join the group
- We could know that after the end of loop, the map should be empty