Skip to content
LC-1282 Medium LeetCode

1282. Group the People Given the Group Size They Belong To

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 87% Topics: Array, Hash Table, Greedy
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(n)

import collections


class Solution(object):
    def groupThePeople(self, groupSizes):
        """
        :type groupSizes: List[int]
        :rtype: List[List[int]]
        """
        groups, result = collections.defaultdict(list), []
        for i, size in enumerate(groupSizes):
            groups[size].append(i)
            if len(groups[size]) == size:
                result.append(groups.pop(size))
        return result

Solution from kamyu104/LeetCode-Solutions · MIT