forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroup the People Given the Group Size They Belong To.java
35 lines (34 loc) · 1.36 KB
/
Group the People Given the Group Size They Belong To.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
35
// Runtime: 54 ms (Top 5.31%) | Memory: 53.9 MB (Top 61.36%)
class Solution {
public List<List<Integer>> groupThePeople(int[] groupSizes) {
List<List<Integer>> temp = new ArrayList<List<Integer>>();
List<List<Integer>> result = new ArrayList<List<Integer>>();
for(int i = 0; i<groupSizes.length; i++){
int k = groupSizes[i];
boolean flag = true;
for(int j = 0; j<temp.size(); j++){
// If there is a list of reqired group size and it is filled lesser than we can put element in that one
if(k == temp.get(j).get(0) && k >temp.get(j).get(1)){
result.get(j).add(i);
temp.get(j).set(1,temp.get(j).get(1)+1);
flag=false;
break;
}
}
if(flag){
// comment 1
// We create a list with index and put it to result
List<Integer> res = new ArrayList();
res.add(i);
result.add(res);
// comment 2
// we create a new list recording max value can stored and currently filled
List<Integer> tempRes = new ArrayList();
tempRes.add(k);
tempRes.add(1);
temp.add(tempRes);
}
}
return result;
}
}