#1103
Distribute Candies to People
pupil · 325 · lc easy +22 · verified · 67.5% accepted · 1,025 likes · top 74%
Description
Distribute candies to num_people people standing in a row as follows: give 1 candy to the first person, 2 to the second, and so on up to num_people, then wrap around and give num_people + 1 to the first person again, continuing this pattern. The last recipient gets all remaining candies.
Return an array of length num_people showing the final candy count for each person.
Example 1:
Input: candies = 7, num_people = 4
Output: [1,2,3,1]
Explanation:
On the first turn, ans[0] += 1, and the array is [1,0,0,0].
On the second turn, ans[1] += 2, and the array is [1,2,0,0].
On the third turn, ans[2] += 3, and the array is [1,2,3,0].
On the fourth turn, ans[3] += 1 (because there is only one candy left), and the final array is [1,2,3,1].
Example 2:
Input: candies = 10, num_people = 3
Output: [5,2,3]
Explanation:
On the first turn, ans[0] += 1, and the array is [1,0,0].
On the second turn, ans[1] += 2, and the array is [1,2,0].
On the third turn, ans[2] += 3, and the array is [1,2,3].
On the fourth turn, ans[0] += 4, and the final array is [5,2,3].
Code
1
2
3