Skip to content

Latest commit

 

History

History
36 lines (25 loc) · 954 Bytes

1282-kir3i.md

File metadata and controls

36 lines (25 loc) · 954 Bytes

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

Solution

  • 시간복잡도: O(N)

  • 알고리즘

    구현, 자료구조

  • 풀이설명

    1. hash table을 이용해 match에 각 사람이 어떤 크기의 그룹에 속하고 싶은지 저장합니다. 이 때 key는 groupSize로, value는 사람 번호로 저장했습니다.
    2. match가 완성되면 저장된 값들을 조회하며 groupSize만큼 묶어서 ans에 추가합니다.
  • 소스코드

class Solution:
    def groupThePeople(self, groupSizes):
        match = {}
        for idx, groupSize in enumerate(groupSizes):
            if groupSize in match:
                match[groupSize].append(idx)
            else:
                match[groupSize] = [idx]

        ans = []
        for groupSize, m in match.items():
            while m:
                ans.append(m[:groupSize])
                m = m[groupSize:]
        
        return ans