给出数组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