LeetCode 1103. Distribute Candies to People (分糖果 II)

题目标签:Math

  题目让我们分发糖果,分的糖果从1 开始依次增加,直到分完。

  for loop可以计数糖果的数量,直到糖果发完。但是还是要遍历array 给people 发糖,这里要用到 index = (本轮分糖果的量 % people 的人数)糖果的数量从0 开始计数,这样的话,index 就会一直重复遍历 array,具体看code。

Java Solution:

Runtime:  1ms, faster than 90.53%

Memory Usage: 33.8 MB, less than 100.00%

完成日期:07/15/2019

关键点:利用%重复遍历array

class Solution {
    public int[] distributeCandies(int candies, int num_people) {
        int[] people = new int[num_people];

        for(int give = 0; candies > 0; candies -= give) {
            people[give % num_people] += Math.min(candies, ++give);
        }

        return people;
    }
}

参考资料:LeetCode discuss

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

原文地址:https://www.cnblogs.com/jimmycheng/p/11525277.html

时间: 2024-08-29 08:02:33

LeetCode 1103. Distribute Candies to People (分糖果 II)的相关文章

[LeetCode] 1103. Distribute Candies to People 分糖果

题目: 思路: 本题一开始的思路就是按照流程一步步分下去,算是暴力方法,在官方题解中有利用等差数列进行计算的 这里只记录一下自己的暴力解题方式 只考虑每次分配的糖果数,分配的糖果数为1,2,3,4,5,..., 依次加1 再考虑到分配的轮数,可以利用 i % num_people 来求得第i次应该分配到第几个人 最后要注意的是,如果当前糖果数小于本应该分配的糖果数,则将当前糖果全部给予,也就是要判断剩余糖果数 candies 与本该分配糖果数 i+1 的大小,谁小分配谁 代码: class So

LeetCode——1103. 分糖果 II

排排坐,分糖果. 我们买了一些糖果 candies,打算把它们分给排好队的 n = num_people 个小朋友. 给第一个小朋友 1 颗糖果,第二个小朋友 2 颗,依此类推,直到给最后一个小朋友 n 颗糖果. 然后,我们再回到队伍的起点,给第一个小朋友 n + 1 颗糖果,第二个小朋友 n + 2 颗,依此类推,直到给最后一个小朋友 2 * n 颗糖果. 重复上述过程(每次都比上一次多给出一颗糖果,当到达队伍终点后再次从队伍起点开始),直到我们分完所有的糖果.注意,就算我们手中的剩下糖果数不

力扣——分糖果 II

排排坐,分糖果. 我们买了一些糖果 candies,打算把它们分给排好队的 n = num_people 个小朋友. 给第一个小朋友 1 颗糖果,第二个小朋友 2 颗,依此类推,直到给最后一个小朋友 n 颗糖果. 然后,我们再回到队伍的起点,给第一个小朋友 n + 1 颗糖果,第二个小朋友 n + 2 颗,依此类推,直到给最后一个小朋友 2 * n 颗糖果. 重复上述过程(每次都比上一次多给出一颗糖果,当到达队伍终点后再次从队伍起点开始),直到我们分完所有的糖果.注意,就算我们手中的剩下糖果数不

LeetCode 575. Distribute Candies (发糖果)

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister.

LeetCode - 575. Distribute Candies

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister.

LeetCode.1103-向人们分发糖果(Distribute Candies to People)

这是小川的第393次更新,第425篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第256题(顺位题号是1103).我们通过以下方式向一排n = num_people个人分发一些糖果: 给第一个人送1个糖果,给第二个人送2个糖果,依此类推,直到我们给最后一个人送糖果.然后,我们回到行的开头,向第一个人提供n + 1个糖果,向第二个人提供n + 2个糖果,依此类推,直到我们向最后一个人提供2 * n个糖果. 这个过程重复进行,直到我们用完糖果.最后一个人将得到所有剩余的

[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现

[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least

[最短路]2197 分糖果

题目描述 Description 童年的我们,将和朋友分享美好的事物作为自己的快乐.这天,C小朋友得到了Plenty of candies,将要把这些糖果分给要好的朋友们.已知糖果从一个人传给另一个人需要1 秒的时间,同一个小朋友不会重复接受糖果.由于糖果足够多,如果某时刻某小朋友接受了糖果,他会将糖果分成若干份,分给那些在他身旁且还没有得到糖果的小朋友们,而且自己会吃一些糖果.由于嘴馋,小朋友们等不及将糖果发完,会在得到糖果后边吃边发.每个小朋友从接受糖果到吃完糖果需要m秒的时间.那么,如果第

575. Distribute Candies

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister.