LeetCode 1103. 分糖果 II Distribute Candies to People

简介: LeetCode 1103. 分糖果 II Distribute Candies to People

LeetCode 1103. 分糖果 II Distribute Candies to People


Table of Contents

 

一、中文版

二、英文版

三、My answer

四、解题报告


一、中文版

排排坐,分糖果。

我们买了一些糖果 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


二、英文版

We distribute some number of candies, to a row of n = num_people people in the following way:
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.
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.
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].
Constraints:
1 <= candies <= 10^9
1 <= num_people <= 1000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/distribute-candies-to-people
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


三、My answer

class Solution:
    def distributeCandies(self, candies: int, num_people: int) -> List[int]:
        ans = [0] * num_people
        candy = 0
        base = 1 # 递增加1
        while candy < candies:
            for i in range(num_people):
                if candy + base <= candies:
                    ans[i] += base                    
                    candy += base
                    base += 1
                else:
                    ans[i] += (candies - candy)
                    candy += (candies - candy)
            # i = 0 因为不只遍历一遍数组,所以临时糖果总数candy小于总数 candies 时会重头开始for 循环
        return ans


四、解题报告

模拟题,直接按照题意 code 即可。


相关文章
|
1月前
|
测试技术
力扣888 公平糖果交换
力扣888 公平糖果交换
|
8月前
【Leetcode -575.分糖果 -594.最长和谐子序列】
【Leetcode -575.分糖果 -594.最长和谐子序列】
41 0
|
20天前
|
存储 算法 数据可视化
如何使用多种算法解决LeetCode第135题——分发糖果问题
如何使用多种算法解决LeetCode第135题——分发糖果问题
|
2天前
|
算法
力扣经典150题第十五题:分发糖果
力扣经典150题第十五题:分发糖果
7 0
|
5天前
LeetCode575——分糖果
LeetCode575——分糖果
10 0
|
24天前
|
算法 Java Go
【经典算法】LeetCode 1103 分糖果 II(Java/C/Python3实现含注释说明,Easy)
【经典算法】LeetCode 1103 分糖果 II(Java/C/Python3实现含注释说明,Easy)
19 0
|
1月前
|
索引
[经典力扣面试题]135. 分发糖果
[经典力扣面试题]135. 分发糖果
|
1月前
|
算法 vr&ar 图形学
☆打卡算法☆LeetCode 135. 分发糖果 算法解析
☆打卡算法☆LeetCode 135. 分发糖果 算法解析
|
1月前
LeetCode 20200601 打卡 1431. 拥有最多糖果的孩子
LeetCode 20200601 打卡 1431. 拥有最多糖果的孩子
15 0
|
7月前
|
算法 网络架构
代码随想录算法训练营第三十三天 | LeetCode 1005. K 次取反后最大化的数组和、134. 加油站、135. 分发糖果
代码随想录算法训练营第三十三天 | LeetCode 1005. K 次取反后最大化的数组和、134. 加油站、135. 分发糖果
41 0