給出數組groupSizes,每個元素代表第i個人屬于size為groupSizes[i]的組,輸出每個組的組成
用python的dict存每個不同size的group的成員,然后分到不同list輸出
1 #1282
2 #Runtime: 55 ms (Beats 33.33%)
3 #Memory: 13.3 MB (Beats 84.85%)
4
5 class Solution(object):
6 def groupThePeople(self, groupSizes):
7 """
8 :type groupSizes: List[int]
9 :rtype: List[List[int]]
10 """
11 ans = []
12 dic = defaultdict(list)
13 for i, sz in enumerate(groupSizes):
14 dic[sz].append(i)
15 for k, lst in dic.items():
16 for i in range(0, len(lst), k):
17 ans.append(lst[i : i + k])
18 return ans