[LeetCode] 1103. Distribute Candies to People 分糖果
题目:
思路:
本题一开始的思路就是按照流程一步步分下去,算是暴力方法,在官方题解中有利用等差数列进行计算的
这里只记录一下自己的暴力解题方式
只考虑每次分配的糖果数,分配的糖果数为1,2,3,4,5,..., 依次加1
再考虑到分配的轮数,可以利用 i % num_people 来求得第i次应该分配到第几个人
最后要注意的是,如果当前糖果数小于本应该分配的糖果数,则将当前糖果全部给予,也就是要判断剩余糖果数 candies 与本该分配糖果数 i+ 的大小,谁小分配谁
代码:
class Solution {
public:
vector<int> distributeCandies(int candies, int num_people) {
// vector<int> res(num_people, 0);
// int i=1;
// for(int k=0; candies>=i+k*num_people; k++){
// while(i <= num_people){
// if(candies<i+k*num_people){
// res[i-1] += candies;
// candies = 0;
// break;
// }
// res[i-1] += i+k*num_people;
// candies -= i+k*num_people;
// i++;
// }
// i=1;
// }
// if(candies) res[0] += candies;
// return res;
vector<int> ans(num_people); // 初始元素为0
int i=;
while(candies!=){
ans[i % num_people] += min(candies, i+);
candies -= min(candies, i+);
i++;
}
return ans;
}
};
被注释的代码是自己的想法,虽然复杂度相同,看到题解才发现自己小题大做,没有理解透题目
[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
1103. Distribute Candies to People(分糖果||) 链接:https://leetcode-cn.com/problems/distribute-candies-to- ...
- 【Leetcode_easy】1103. Distribute Candies to People
problem 1103. Distribute Candies to People solution:没看明白代码... class Solution { public: vector<int ...
- LeetCode 575. Distribute Candies (发糖果)
Given an integer array with even length, where different numbers in this array represent different k ...
- 【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 differ ...
- LeetCode: 575 Distribute Candies(easy)
题目: Given an integer array with even length, where different numbers in this array represent differe ...
- LeetCode.1103-向人们分发糖果(Distribute Candies to People)
这是小川的第393次更新,第425篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第256题(顺位题号是1103).我们通过以下方式向一排n = num_people个人分 ...
- [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现
[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...
随机推荐
- com.google.zxing:core 生成二维码的简单使用
String content = ""; int size = 240; Hashtable<EncodeHintType, String> hints = new H ...
- Linux系统安装及配置ftp服务
1. 先用rpm -qa| grep vsftpd命令检查是否已经安装,如果ftp没有安装,使用yum -y install vsftpd 安装,(ubuntu 下使用apt-get instal ...
- 数论入门——斐蜀定理与拓展欧几里得算法
斐蜀定理 内容 斐蜀定理又叫贝祖定理,它的内容是这样的: 若$a,bin N$,那么对于任意x,y,方程$ax+by=gcd(a,b)*k(kin N)$一定有解,且一定有一组解使$ax+by=gcd ...
- 100)PHP,文件上传总代码整理
首先是我的目录关系: 然后我的html表单代码: <html> <head> <title>Form</title> <meta http-equ ...
- 新iPhone又要提价,苹果靠什么基业长青?
在股神巴菲特一番煽情言论之后,苹果股价再创新高,达到187.67美元,总市值约为9450亿美元,正大踏步向着1万亿美元市值的目标前进,这是一条科技.经济.财经.社会等领域的头条新闻,遭到全球各界人士的 ...
- leaflet本地加载arcgis切片
var origin = [-20037700, 33711400];//图层起点坐标 var resolutions = [ 8466.6836000338681, // Level 0 4233. ...
- Jquery中的offset()和position()深入剖析
jquery 中有两个获取元素位置的方法offset()和position(),这两个方法之间有什么异同?使用的时候应该注意哪些问题?什么时候使用offset(),什么时候又使用position()呢 ...
- 爬虫时伪装header信息
在爬虫时,一般需要伪装Agent信息,放在header中 1.header不是必传参数,在需要的时候进行伪装 2.header = {"User-Agent": "Moz ...
- Sublime Text2 使用心得总结
sublime text2是开发代码编辑的神器 ,编辑器界面优美,操作速度快速.而且Sublime Text2是一款跨平台的编辑器,再也不用为换平台而找不到合适的.熟悉的编辑器担忧了. Sublime ...
- [hdu4630] No Pain No Game
某次模拟赛的T1. 刚开始怀疑是RMQ......我真是太弱了QAQ 题目传送门 正解是离线操作,把所有询问按r从小到大排序. 然后把数从左到右处理,处理完第i个数,就可以回答所有r==i的询问了. ...
