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
| class Solution { public List<List<Integer>> groupThePeople(int[] groupSizes) { Map<Integer, List<Integer>> map = new HashMap<>(); for(int i = 0; i < groupSizes.length; i++){ if(map.containsKey(groupSizes[i])){ map.get(groupSizes[i]).add(i); }else{ List<Integer> tmp = new ArrayList<>(); tmp.add(i); map.put(groupSizes[i], tmp); } } List<List<Integer>> result = new ArrayList<>();
for(Map.Entry<Integer, List<Integer>> entry : map.entrySet()){ int size = entry.getKey(); List<Integer> list = entry.getValue(); if(list.size()>size){ int l = 0; int r = size; for (int i = 0; i < list.size()/size; i++) { result.add(list.subList(l, r)); l = r; r = r + size; } }else{ result.add(entry.getValue()); } } return result; } }
|