|
1 | 1 | classSolution {
|
2 |
| -publicList<List<Integer>>groupThePeople(int[]groupSizes) { |
3 |
| -Map<Integer,Queue<Integer>>groupSizeToId =newHashMap<>(); |
4 |
| -for (inti =0;i <groupSizes.length;i++) { |
5 |
| -groupSizeToId.computeIfAbsent(groupSizes[i],k ->newLinkedList<>()).add(i); |
6 |
| - } |
7 |
| -List<List<Integer>>result =newArrayList<>(); |
8 |
| -for (IntegergroupSize :groupSizeToId.keySet()) { |
9 |
| -Queue<Integer>ids =groupSizeToId.get(groupSize); |
10 |
| -while (!ids.isEmpty()) { |
11 |
| -List<Integer>group =newArrayList<>(groupSize); |
12 |
| -for (inti =0;i<groupSize;i++) { |
13 |
| -group.add(ids.remove()); |
| 2 | +publicList<List<Integer>>groupThePeople(int[]groupSizes) { |
| 3 | +Map<Integer,List<Integer>>map =newHashMap<>(); |
| 4 | +for (inti =0;i <groupSizes.length;i++) { |
| 5 | +map.computeIfAbsent(groupSizes[i],k ->newArrayList<>()).add(i); |
| 6 | + } |
| 7 | +List<List<Integer>>result =newArrayList<>(); |
| 8 | +for (Integerkey :map.keySet()) { |
| 9 | +List<Integer>candidates =map.get(key); |
| 10 | +intidx =0; |
| 11 | +while (idx <candidates.size()) { |
| 12 | +result.add(candidates.subList(idx,idx +key)); |
| 13 | +idx +=key; |
| 14 | + } |
14 | 15 | }
|
15 |
| -result.add(group); |
16 |
| - } |
| 16 | +returnresult; |
17 | 17 | }
|
18 |
| -returnresult; |
19 |
| - } |
20 | 18 | }
|