728x90
https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/
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
'IT > 코딩테스트' 카테고리의 다른 글
[Leetcode] 1365. How Many Numbers Are Smaller Than the Current Number (0) | 2024.02.09 |
---|---|
[Leetcode] 2610. Convert an Array Into a 2D Array With Conditions (0) | 2024.02.08 |
[Leetcode] 771. Jewels and Stones (0) | 2024.02.07 |
[Leetcode] 1512. Number of Good Pairs (0) | 2024.02.07 |
[Leetcode] 2671. Frequency Tracker (0) | 2024.02.07 |