1 class Solution: 2 def distributeCandies(self, candies: int, num_people: int) -> 'List[int]': 3 res = [0] * num_people 4 t = 0 5 while candies >= 0: 6 for i in range(num_people): 7 t += 1 8 if candies >= t: 9 res[i] += t10 candies -= t11 else:12 res[i] += candies13 return res14 return res15 16
两层循环,外层循环每次增加分配的数量t,内层循环进行分配减少candies的数量。