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

这是小川的第393次更新,第425篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第256题(顺位题号是1103)。我们通过以下方式向一排n = num_people个人分发一些糖果:

给第一个人送1个糖果,给第二个人送2个糖果,依此类推,直到我们给最后一个人送糖果。然后,我们回到行的开头,向第一个人提供n + 1个糖果,向第二个人提供n + 2个糖果,依此类推,直到我们向最后一个人提供2 * n个糖果。

这个过程重复进行,直到我们用完糖果。最后一个人将得到所有剩余的糖果(不一定比之前收到的多)。

返回一个数组(长度为num_people,元素总和为candies),代表糖果的最终分配结果。

例如:

输入: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,2,3,1]。

输入: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

02 第一种解法

暴力解法。

直接使用两层循环,外层控制candies的剩余量,内层循环n次,定义一个变量sum,从1开始自增,代表每次要分出去的糖果数量,内层循环中,每遍历一次,sum加1,同时candies要减去sum,如果最后剩余的糖果小于了本次预计要分配的数量,就将剩余的糖果全给当前这个人,candies为0,循环结束。

public int[] distributeCandies(int candies, int num_people) {
    int[] result = new int[num_people];
    int sum = 1;
    while (candies > 0) {
        for (int i=0; i<result.length; i++) {
            if (candies - sum> 0) {
                result[i] += sum;
                candies -= sum;
                sum++;
            } else {
                result[i] += candies;
                candies = 0;
                break;
            }
        }
    }
    return result;
}

03 第二种解法

我们还可以对第一种解法再优化下,变成一层循环,借助取余来实现。

因为每执行一次从头到尾的分配,都是从第一个人到第n个人,可以利用数组的下标对n取余来替代,其他处理逻辑不变。

public int[] distributeCandies2(int candies, int num_people) {
    int[] result = new int[num_people];
    int sum = 1;
    for (int i=0; candies > 0; i++) {
        if (candies - sum> 0) {
            result[i%num_people] += sum;
            candies -= sum;
            sum++;
        } else {
            result[i%num_people] += candies;
            break;
        }
    }
    return result;
}

04 第三种解法

我们还可以对第二种解法再优化下,省掉循环方法体里面的if判断。

结果数组的索引是从0开始的,代表第一个人,那他被分配的糖果数量是索引值加1,在前面两种解法中,都使用了if判断candies是不是比当前需要分配出去的糖果大,其实就是取两者之间的较小值。

如果candies剩余的数量比当前需要分配出去的糖果数量大,就可以继续分配;如果candies剩余的数量比当前需要分配出去的糖果数量小,说明当前这次分配时最后一次分配,只能将剩余的糖果数量全部分给当前此人了。

public int[] distributeCandies3(int candies, int num_people) {
    int[] result = new int[num_people];
    for (int i=0; candies > 0; i++) {
        result[i%num_people] += Math.min(candies, i+1);
        candies -= i+1;
    }
    return result;
}

05 小结

算法专题目前已连续日更超过八个月,算法题文章262+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

原文地址:https://www.cnblogs.com/xiaochuan94/p/11253765.html

时间: 2024-08-01 05:42:54

LeetCode.1103-向人们分发糖果(Distribute Candies to People)的相关文章

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 (分糖果 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

【LeetCode】分发糖果系列问题(I、II、III)

(一)分糖果 题目(Easy): 575. 分糖果 题目描述: 给定一个偶数长度的数组,其中不同的数字代表着不同种类的糖果,每一个数字代表一个糖果.你需要把这些糖果平均分给一个弟弟和一个妹妹.返回妹妹可以获得的最大糖果的种类数. 示例 : 输入: candies = [1,1,2,2,3,3] 输出: 3 解析: 一共有三种种类的糖果,每一种都有两个. 最优分配方案:妹妹获得[1,2,3],弟弟也获得[1,2,3].这样使妹妹获得糖果的种类数最多. 解题思路: ??本题比较简单,实际上就是将n个

leetcode(135)分发糖果

分发糖果 解题思路:贪心算法.两次遍历 class Solution { public int candy(int[] ratings) { int len = ratings.length; if(len==0){ return 0; } int[] cands = new int[len]; for(int i=0;i<len;i++){ cands[i] = 1; } for(int i=0;i<len-1;i++){ if(ratings[i+1]>ratings[i]&

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

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

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] 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] 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算法题-Distribute Candies(Java实现)

这是悦乐书的第266次更新,第279篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第133题(顺位题号是575).给定具有偶数长度的整数数组,其中该数组中的不同数字表示不同种类的糖果. 每个数字表示相应种类的一种糖果. 您需要将这些糖果平均分配给哥哥妹妹. 返回妹妹可以获得的最多种类数量的糖果.例如: 输入:糖果= [1,1,2,2,3,3] 输出:3 说明:有三种不同的糖果(1,2和3),每种糖果两种.最佳分配:妹妹有糖果[1,2,3],哥哥也有糖果[1,2,3]