Group the People Given the Group Size They Belong To LeetCode solution in Java

1
class Solution {
2
public List<List<Integer>> groupThePeople(int[] groupSizes) {
3

4
List<List<Integer>> temp = new ArrayList<List<Integer>>();
5
List<List<Integer>> result = new ArrayList<List<Integer>>();
6
for (int i = 0; i < groupSizes.length; i++) {
7
int k = groupSizes[i];
8
boolean flag = true;
9
for (int j = 0; j < temp.size(); j++) {
10
// If there is a list of reqired group size and it is filled lesser than we can put element
11
// in that one
12
if (k == temp.get(j).get(0) && k > temp.get(j).get(1)) {
13
result.get(j).add(i);
14
temp.get(j).set(1, temp.get(j).get(1) + 1);
15
flag = false;
16
break;
17
}
18
}
19
if (flag) {
20
// comment 1
21
// We create a list with index and put it to result
22
List<Integer> res = new ArrayList();
23
res.add(i);
24
result.add(res);
25
// comment 2
26
// we create a new list recording max value can stored and currently filled
27
List<Integer> tempRes = new ArrayList();
28
tempRes.add(k);
29
tempRes.add(1);
30
temp.add(tempRes);
31
}
32
}
33
return result;
34
}
35
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0