본문 바로가기

IT/코딩테스트

[Leetcode] 1282. Group the People Given the Group Size They Belong To

728x90

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

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

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

        for index, value in enumerate(groupSizes):
            groups[value] = groups.get(value, [])
            groups[value].append(index)

        for key, value in groups.items():
            if len(value) > key:
                split_list = [value[i:i+key] for i in range(0, len(value), key)]
                for s in split_list:
                    result.append(s)
            else:
                result.append(value)

        return result
728x90