File tree 1 file changed +14
-16
lines changed
1 file changed +14
-16
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
- public List <List <Integer >> groupThePeople (int [] groupSizes ) {
3
- Map <Integer , Queue <Integer >> groupSizeToId = new HashMap <>();
4
- for (int i = 0 ; i < groupSizes .length ; i ++) {
5
- groupSizeToId .computeIfAbsent (groupSizes [i ], k -> new LinkedList <>()).add (i );
6
- }
7
- List <List <Integer >> result = new ArrayList <>();
8
- for (Integer groupSize : groupSizeToId .keySet ()) {
9
- Queue <Integer > ids = groupSizeToId .get (groupSize );
10
- while (!ids .isEmpty ()) {
11
- List <Integer > group = new ArrayList <>(groupSize );
12
- for (int i = 0 ; i < groupSize ; i ++) {
13
- group .add (ids .remove ());
2
+ public List <List <Integer >> groupThePeople (int [] groupSizes ) {
3
+ Map <Integer , List <Integer >> map = new HashMap <>();
4
+ for (int i = 0 ; i < groupSizes .length ; i ++) {
5
+ map .computeIfAbsent (groupSizes [i ], k -> new ArrayList <>()).add (i );
6
+ }
7
+ List <List <Integer >> result = new ArrayList <>();
8
+ for (Integer key : map .keySet ()) {
9
+ List <Integer > candidates = map .get (key );
10
+ int idx = 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
+ return result ;
17
17
}
18
- return result ;
19
- }
20
18
}
You can’t perform that action at this time.
0 commit comments