Suppose we want to distribute some number of candies to a row of n people in the following way −
We will repeat this process until we run out of candies. The last people will get all of our remaining candies (not necessarily one more than the previous gift).
We have to return an array that represents the final distribution of candies. So suppose candies are 7, and n = 3, then the output will be [2, 2, 3]. So at first the first person will get 1. the array is [1, 0, 0], second one has got 2, then array is [1, 2, 0], third one has got 3, then array is [1, 2, 3], and finally first one again got 1, so array is [2, 2, 3]
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution(object): def distributeCandies(self, candies, num_people): res = [0 for i in range(num_people)] index = 0 while candies>0: res[index%num_people] += min(candies,index+1) candies-=(index+1) index+=1 return res ob1 = Solution() print(ob1.distributeCandies(8, 3))
[3, 2, 3]