1282. Group the People Given the Group Size They Belong To (Medium) - TengnanYao/daily_leetcode GitHub Wiki

class Solution:
    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:
        result = []
        n = len(groupSizes)
        h = {}
        for i in range(n):
            k = groupSizes[i]
            if k in h:
                h[k].append(i)
            else:
                h[k] = [i]
            if len(h[k]) == k:
                result.append(h[k])
                del h[k]
        return result