1103. Distribute Candies to People(分糖果||)

链接:https://leetcode-cn.com/problems/distribute-candies-to-people/

题目:

  排排坐,分糖果。

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

思路:

  没啥思路,套一个循环,注意改变数量和及时跳出即可。如果想要更简单一点的,可以通过通项公式直接算出该轮所需糖果,直接分配也行。

代码:

 public int[] distributeCandies(int candies, int num_people) {
int[] number = new int[num_people];
int num = 1;
int i = 0;
while (candies > 0) { number[i] += num;
candies -= num;
i++;
if (i == num_people) {
i = 0;
}
if (candies > num + 1) {
num++;
} else {
number[i] += candies;
break;
} }
return number;
}

LeetCode 1103. Distribute Candies to People的更多相关文章

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

    题目标签:Math 题目让我们分发糖果,分的糖果从1 开始依次增加,直到分完. for loop可以计数糖果的数量,直到糖果发完.但是还是要遍历array 给people 发糖,这里要用到 index ...

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

    题目: 思路: 本题一开始的思路就是按照流程一步步分下去,算是暴力方法,在官方题解中有利用等差数列进行计算的 这里只记录一下自己的暴力解题方式 只考虑每次分配的糖果数,分配的糖果数为1,2,3,4,5 ...

  3. 【Leetcode_easy】1103. Distribute Candies to People

    problem 1103. Distribute Candies to People solution:没看明白代码... class Solution { public: vector<int ...

  4. 【leetcode】1103. Distribute Candies to People

    题目如下: We distribute some number of candies, to a row of n = num_people people in the following way: ...

  5. LeetCode 575. Distribute Candies (发糖果)

    Given an integer array with even length, where different numbers in this array represent different k ...

  6. LeetCode 575 Distribute Candies 解题报告

    题目要求 Given an integer array with even length, where different numbers in this array represent differ ...

  7. LeetCode: 575 Distribute Candies(easy)

    题目: Given an integer array with even length, where different numbers in this array represent differe ...

  8. [LeetCode] Distribute Candies 分糖果

    Given an integer array with even length, where different numbers in this array represent different k ...

  9. LeetCode Distribute Candies

    原题链接在这里:https://leetcode.com/problems/distribute-candies/#/description 题目: Given an integer array wi ...

随机推荐

  1. ES--Kibana相关操作创建索引和Mapping

    创建索引 kibana 使用如下命令进行创建索引内容. PUT test_index/ 创建别名 POST _aliases { "actions": [{ "add&q ...

  2. decimal赋值

    decimal dRebate1 = new decimal(1);decimal dRebate2 = Convert.ToDecimal(1);decimal dRebate3 = 1M; htt ...

  3. SurfaceView双缓冲技术引入

    package com.loaderman.customviewdemo; import android.content.Context; import android.graphics.Canvas ...

  4. spring的事务是什么?与数据库的事务是否一样

    spring的事务是什么?与数据库的事务是否一样 先说一下什么是事务,事务:是对数据库的一些列操作. 之前一直觉得事务只针对于数据库当中,5种隔离级别,7种传播行为,后来才发现这是针对Spring的, ...

  5. PHP松散比较与严格比较的区别详解

    在PHP中相等的比较有两种,松散比较和严格比较,当使用松散比较时,如果进行比较的两个操作数类型不同,那么会对操作数进行适当的类型转换,如果转换后的值相同则认为两个操作数相等.而使用严格比较时,如果两个 ...

  6. 解决Jmeter跨线程组取参数值难题!(还没试)

    来源 https://mp.weixin.qq.com/s/q7ArxwnX1sOfa9tfHouSBQ 如果你工作中已经在用jmeter做接口测试,或性能测试了,你可能会遇到一个麻烦.   那就是j ...

  7. 详解VMware 虚拟机中添加新硬盘的方法

    一.VMware新增磁盘的设置步骤 (建议:在设置虚拟的时候,不要运行虚拟机的系统,不然添加了新的虚拟磁盘则要重启虚拟机) 1.选择“VM”----“设置”并打开,将光标定位在“硬盘(SCSI)”这一 ...

  8. 123457123456#0#-----com.twoapp.mathGame13--前拼后广--13种数学方法jiemei

    com.twoapp.mathGame13--前拼后广--13种数学方法jiemei

  9. Java环境中,word文档转PDF直接打开浏览而非下载

    在平台上,需要把文档直接浏览而非下载,实现方法是先把文档转为PDF文件, 但在linux系统中确实汉字字库,所以转换失败,以下是解决方法 后面正式服务器也要添加字库,不然会转换出乱码文件,处理步骤如下 ...

  10. @Qualifier is not applicable for constructor

    问题场景: 笔者在springboot项目中使用java_websocket作为客户端,调用第三方ws服务. 最初只调用一个ws服务,以下代码可以正常工作: @Bean public URI ttsU ...