Problem Solving/LeetCode

2023.09.11 Today's Challenge

fabichoi 2023. 9. 11. 23:45

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

 

Group the People Given the Group Size They Belong To - LeetCode

Can you solve this real interview question? Group the People Given the Group Size They Belong To - There are n people that are split into some unknown number of groups. Each person is labeled with a unique ID from 0 to n - 1. You are given an integer

leetcode.com

해쉬 테이블을 활용해서 풀면 됨

class Solution:
    def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:
        groups, result = {}, []

        for i, size in enumerate(groupSizes):
            if size not in groups:
                groups[size] = []
            groups[size].append(i)

            if len(groups[size]) == size:
                result.append(groups[size])
                groups[size] = []

        return result
반응형