★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ghuehxlr-kz.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
We distribute some number of candies
, to a row of n = num_people
people in the following way:git
We then give 1 candy to the first person, 2 candies to the second person, and so on until we give n
candies to the last person.github
Then, we go back to the start of the row, giving n + 1
candies to the first person, n + 2
candies to the second person, and so on until we give 2 * n
candies to the last person.數組
This process repeats (with us giving one more candy each time, and moving to the start of the row after we reach the end) until we run out of candies. The last person will receive all of our remaining candies (not necessarily one more than the previous gift).微信
Return an array (of length num_people
and sum candies
) that represents the final distribution of candies.spa
Example 1:code
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:htm
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].
Constraints:blog
排排坐,分糖果。rem
咱們買了一些糖果 candies
,打算把它們分給排好隊的 n = num_people
個小朋友。
給第一個小朋友 1 顆糖果,第二個小朋友 2 顆,依此類推,直到給最後一個小朋友 n
顆糖果。
而後,咱們再回到隊伍的起點,給第一個小朋友 n + 1
顆糖果,第二個小朋友 n + 2
顆,依此類推,直到給最後一個小朋友 2 * n
顆糖果。
重複上述過程(每次都比上一次多給出一顆糖果,當到達隊伍終點後再次從隊伍起點開始),直到咱們分完全部的糖果。注意,就算咱們手中的剩下糖果數不夠(不比前一次發出的糖果多),這些糖果也會所有發給當前的小朋友。
返回一個長度爲 num_people
、元素之和爲 candies
的數組,以表示糖果的最終分發狀況(即 ans[i]
表示第 i
個小朋友分到的糖果數)。
示例 1:
輸入:candies = 7, num_people = 4 輸出:[1,2,3,1] 解釋: 第一次,ans[0] += 1,數組變爲 [1,0,0,0]。 第二次,ans[1] += 2,數組變爲 [1,2,0,0]。 第三次,ans[2] += 3,數組變爲 [1,2,3,0]。 第四次,ans[3] += 1(由於此時只剩下 1 顆糖果),最終數組變爲 [1,2,3,1]。
示例 2:
輸入: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]。
提示:
1 <= candies <= 10^9
1 <= num_people <= 1000
1 class Solution { 2 func distributeCandies(_ candies: Int, _ num_people: Int) -> [Int] { 3 var distribution = Array(repeating: 0, count: num_people) 4 5 var step = 0 6 var remaining = candies 7 while 0 < remaining { 8 let index = step % distribution.count 9 let amount = min(step + 1, remaining) 10 distribution[index] += amount 11 remaining -= amount 12 step += 1 13 } 14 return distribution 15 } 16 }
Runtime: 8 ms
1 class Solution { 2 func distributeCandies(_ candies: Int, _ num_people: Int) -> [Int] { 3 var candies = candies 4 var a:[Int] = [Int](repeating:0,count:num_people) 5 var p:Int = 0 6 while(candies > 0) 7 { 8 let x:Int = min(candies, p + 1) 9 a[p % num_people] += x 10 candies -= x 11 p += 1 12 } 13 return a 14 } 15 }