LeetCode 1103. Distribute Candies to People
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的更多相关文章
- LeetCode 1103. Distribute Candies to People (分糖果 II)
题目标签:Math 题目让我们分发糖果,分的糖果从1 开始依次增加,直到分完. for loop可以计数糖果的数量,直到糖果发完.但是还是要遍历array 给people 发糖,这里要用到 index ...
- [LeetCode] 1103. Distribute Candies to People 分糖果
题目: 思路: 本题一开始的思路就是按照流程一步步分下去,算是暴力方法,在官方题解中有利用等差数列进行计算的 这里只记录一下自己的暴力解题方式 只考虑每次分配的糖果数,分配的糖果数为1,2,3,4,5 ...
- 【Leetcode_easy】1103. Distribute Candies to People
problem 1103. Distribute Candies to People solution:没看明白代码... class Solution { public: vector<int ...
- 【leetcode】1103. Distribute Candies to People
题目如下: We distribute some number of candies, to a row of n = num_people people in the following way: ...
- LeetCode 575. Distribute Candies (发糖果)
Given an integer array with even length, where different numbers in this array represent different k ...
- LeetCode 575 Distribute Candies 解题报告
题目要求 Given an integer array with even length, where different numbers in this array represent differ ...
- LeetCode: 575 Distribute Candies(easy)
题目: Given an integer array with even length, where different numbers in this array represent differe ...
- [LeetCode] Distribute Candies 分糖果
Given an integer array with even length, where different numbers in this array represent different k ...
- LeetCode Distribute Candies
原题链接在这里:https://leetcode.com/problems/distribute-candies/#/description 题目: Given an integer array wi ...
随机推荐
- TynSerial自定义对象的序列(还原)
TynSerial自定义对象的序列(还原) TynSerial是咏南中间件封装的支持数据二进制序列(还原)的类. 对于ORM来说,序列类必须序列(还原)自定义对象. 1)定义一个自定义对象 type ...
- arcgis python 获得打印机
class ToolValidator: """Class for validating a tool's parameter values and controllin ...
- DataSync 异构数据同步
RAC, Data Gurad, Stream 是Oracle 高可用性体系中的三种工具,每个工具即可以独立应用,也可以相互配合. 他们各自的侧重点不同,适用场景也不同. RAC 它的强项在于解决单点 ...
- UML部署图介绍
https://www.w3cschool.cn/uml_tutorial/uml_tutorial-mi5w28ur.html
- 【集成模型】Bootstrap Aggregating(Bagging)
0 - 思想 如下图所示,Bagging(Bootstrap Aggregating)的基本思想是,从训练数据集中有返回的抽象m次形成m个子数据集(bootstrapping),对于每一个子数据集训练 ...
- oracle库两个表关联查询时用 count 报错【我】
oracle数据库,需要对两个表进行关联查询(根据两个字段),结果发现关联后不能改为 count 获取数量,报错如下: 同样的sql换到另外一个数据库就可以(只是因为数据量在千万级,所以很慢,用时40 ...
- Qt编写控件属性设计器4-加载属性
一.前言 控件能加载拖曳拉伸了,这些都是基本的前提工作,接下来的重点就是要动态加载选中控件的属性了,Qt的属性机制那是异常的强大,只能用强大到爆来形容,Qt中编写自定义控件,如果属性都用Q_PROPE ...
- No WebApplicationContext found: no ContextLoaderListener registered
修改前运行报错:No WebApplicationContext found: no ContextLoaderListener registered? <web-app> <dis ...
- mybatis/tk mybatis下实体字段是关键字/保留字,执行报错
实体如下: import com.fasterxml.jackson.annotation.JsonFormat; import com.xxx.web.bean.PagesStatic; impor ...
- Jsoup-简单爬取知乎推荐页面(附:get_agent())
总览 今天我们就来小用一下Jsoup,从一个整体的角度来看一看爬虫 一个基本的爬虫框架包括: [x] 解析网页 [x] 失败重试 [x] 抓取内容保存至本地 [x] 多线程抓取 *** 分模块讲解 将 ...