Article Directory
Problem Description
Cruncher, sub-candy.
We bought some candy candies, they are going to give queued n = num_people two children.
A child to the first confectionery, the second two children, and so on, until the last child to n pieces of candy.
Then, we go back to the starting point of the team, to the first pieces of candy children n + 1, n + 2 second child stars, and so on, until the last child to 2 * n pieces of candy.
Repeat the process (and more each time than the last given a candy, when the team arrived at the end from the beginning starting point for the team again) until we finish all the sub-candy. Note that even if the rest is not in our hands the number of candy (candy before issuing more than once), these candies will be distributed to all current children.
Returns an array of NUM_PEOPLE length, and for the elements of the candies to represent the case of candies final distribution (i.e. ANS [i] represents the number of i-th candy assigned to children).
Examples
输入:candies = 10, num_people = 3
输出:[5,2,3]
解释:
第一次,ans[0] += 1,数组变为 [1,0,0]。
第二次,ans[1] += 2,数组变为 [1,2,0]。
第三次,ans[2] += 3,数组变为 [1,2,3]。
第四次,ans[0] += 4,最终数组变为 [5,2,3]。
Problem solution 1- Violence Act
class Solution:
def distributeCandies(self, candies: int, num_people: int) -> List[int]:
ans=[0]*num_people
i=0
while candies != 0:
ans[i%num_people]+=min((i+1),candies)
candies-=min((i+1),candies)
i+=1
return ans