Leetcode 1407 Solution

This article provides solution to leetcode question 1407 (group-the-people-given-the-group-size-they-belong-to)

https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to

Solution

class Solution:
    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:
        group_ids = collections.defaultdict(list)

        for i, group_size in enumerate(groupSizes):
            group_ids[group_size].append(i)

        ans = []
        for group_size, id_list in sorted(group_ids.items()):
            i = 0
            while i < len(id_list):
                ids = id_list[i:i + group_size]
                ans.append(ids)
                i += group_size
        return ans