Skip to content

Latest commit

 

History

History
34 lines (27 loc) · 923 Bytes

File metadata and controls

34 lines (27 loc) · 923 Bytes

LeetCode Records - Question 1282 Group the People Given the Group Size They Belong To

Attempt 1: Use a HashMap to save the current different sizes of groups

class Solution {
    public List<List<Integer>> groupThePeople(int[] groupSizes) {
        Map<Integer, List<Integer>> map = new HashMap<>();
        List<List<Integer>> answer = new ArrayList<>();

        for (int i = 0; i < groupSizes.length; i++) {
            int size = groupSizes[i];
            List<Integer> list = map.get(size);

            if (list == null) {
                list = new ArrayList<>();
                map.put(size, list);
            }

            list.add(i);

            if (list.size() == size) {
                answer.add(list);
                map.remove(size);
            }
        }

        return answer;
    }
}
  • Runtime: 5 ms (Beats: 92.60%)
  • Memory: 45.40 MB (Beats: 15.36%)