1103. Distribute Candies to People(python 3)

Description:

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.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/distribute-candies-to-people
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • 分糖果。递增数列。第一个人1个,第二个人2个...若人数有限,遍历一遍回到第一个人继续往下增加,直到余下糖果给最后一个人无。

  • QAQ 看教程才会暴力解题

class Solution:
    def distributeCandies(self, candies: int, num_people: int) -> List[int]:
        ans = [0] * num_people
        n = 0
        while candies != 0:
            ans[n % num_people] += min(n+1, candies)
            candies -= min(n+1, candies)
            n += 1
        return ans
  • 要学会灵活使用条件以及 min,max这种语句。

  • leetcode 比较项目里还包括执行用时及执行复杂度。加油!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 篮球赛的准备工作已经差不多了,饮料,毛巾等基本物品都已齐备,圣戈班的的也到了。看着人家的身材,一看就知道是经常运动...
    骑驴书生阅读 541评论 0 51
  • 健康小贴士 夏天是蚊虫多,防蚊灭蚊不能马虎: ①清除家里各类垃圾,尤其是积水容器,水生植物每3-5天换一次水,清洗...
    紫色蒲公英_2阅读 91评论 0 3
  • 一、教学目标 知识与技能目标:通过制定一个主题网站计划,掌握网站设计的基本流程与方法。在确定网站主题、设计栏...
    梦幻天堂晓阅读 408评论 0 2
  • 窃以为,有主意和有主见是绝对不同的。在日常语境中,如果说一个人有主意,一般意思是这人虽然表面不说,但是肚子里蔫有主...
    壁菠盒子阅读 1,743评论 1 4