文章作者:Tyan 博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
**解析:**Version 1,循环遍历即可,current
为当前这次要分配的糖果数量,每次要分配的糖果数量加1,如果不够,则剩余糖果分配给最后一个。
- Version 1
class Solution:
def distributeCandies(self, candies: int, num_people: int) -> List[int]:
result = [0] * num_people
index = 0
current = 1
while candies > 0:
current = min(current, candies)
result[index % num_people] = current
candies -= current
current = 1
index = 1
return result
**解析:**Version 2,index
可以用current
替代。
- Version 2
class Solution:
def distributeCandies(self, candies: int, num_people: int) -> List[int]:
result = [0] * num_people
current = 1
while candies > 0:
result[(current - 1) % num_people] = min(current, candies)
candies -= current
current = 1
return result
Reference
- https://leetcode.com/problems/distribute-candies-to-people/