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.
Input: groupSizes =[3,3,3,3,3,1,3]Output: [[5],[0,1,2],[3,4,6]]Explanation:
The first group is[5]. The size is1, and groupSizes[5]=1.The second group is[0,1,2]. The size is3, and groupSizes[0]= groupSizes[1]= groupSizes[2]=3.The third group is[3,4,6]. The size is3, and groupSizes[3]= groupSizes[4]= groupSizes[6]=3.Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]].
We can use a hash map to collect people by their required group size. As soon as we collect enough people for a group, we finalize that group and start a new one. This greedy approach ensures all constraints are satisfied.
classSolution {
public List<List<Integer>>groupThePeople(int[] groupSizes) {
Map<Integer, List<Integer>> mp =new HashMap<>();
List<List<Integer>> ans =new ArrayList<>();
for (int i = 0; i < groupSizes.length; i++) {
int sz = groupSizes[i];
mp.computeIfAbsent(sz, k ->new ArrayList<>()).add(i);
if (mp.get(sz).size() == sz) {
ans.add(new ArrayList<>(mp.get(sz)));
mp.get(sz).clear();
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
fungroupThePeople(groupSizes: IntArray): List<List<Int>> {
val mp = mutableMapOf<Int, MutableList<Int>>()
val ans = mutableListOf<List<Int>>()
for (i in groupSizes.indices) {
val sz = groupSizes[i]
mp.getOrPut(sz) { mutableListOf() }.add(i)
if (mp[sz]!!.size == sz) {
ans.add(mp[sz]!!.toList())
mp[sz]!!.clear()
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
defgroupThePeople(self, groupSizes: list[int]) -> list[list[int]]:
from collections import defaultdict
mp: dict[int, list[int]] = defaultdict(list)
ans: list[list[int]] = []
for i, sz in enumerate(groupSizes):
mp[sz].append(i)
if len(mp[sz]) == sz:
ans.append(mp[sz][:])
mp[sz].clear()
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
impl Solution {
pubfngroup_the_people(group_sizes: Vec<i32>) -> Vec<Vec<i32>> {
use std::collections::HashMap;
letmut mp: HashMap<i32, Vec<i32>>= HashMap::new();
letmut ans = Vec::new();
for (i, &sz) in group_sizes.iter().enumerate() {
let e = mp.entry(sz).or_insert(Vec::new());
e.push(i asi32);
if e.len() == sz asusize {
ans.push(e.clone());
e.clear();
}
}
ans
}
}