-
Notifications
You must be signed in to change notification settings - Fork 14
/
Solution1282.java
34 lines (29 loc) · 1.08 KB
/
Solution1282.java
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
package leetcode.hashmap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Solution1282 {
public List<List<Integer>> groupThePeople(int[] groupSizes) {
List<List<Integer>> res = new ArrayList<>();
HashMap<Integer, ArrayList<Integer>> sizeGroup = new HashMap<>();
for (int i = 0; i < groupSizes.length; i++) {
if (sizeGroup.containsKey(groupSizes[i])) {
ArrayList<Integer> group = sizeGroup.get(groupSizes[i]);
if (group.size() == groupSizes[i]) {
ArrayList<Integer> newGroup = new ArrayList<>();
newGroup.add(i);
sizeGroup.put(group.size(), newGroup);
res.add(newGroup);
} else {
group.add(i);
}
} else {
ArrayList<Integer> newGroup = new ArrayList<>();
newGroup.add(i);
sizeGroup.put(groupSizes[i], newGroup);
res.add(newGroup);
}
}
return res;
}
}