题目:


思路:

本题一开始的思路就是按照流程一步步分下去,算是暴力方法,在官方题解中有利用等差数列进行计算的

这里只记录一下自己的暴力解题方式

只考虑每次分配的糖果数,分配的糖果数为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 分糖果的更多相关文章

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

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

  2. LeetCode 1103. Distribute Candies to People

    1103. Distribute Candies to People(分糖果||) 链接:https://leetcode-cn.com/problems/distribute-candies-to- ...

  3. 【Leetcode_easy】1103. Distribute Candies to People

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

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

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

  5. 【leetcode】1103. Distribute Candies to People

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

  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.1103-向人们分发糖果(Distribute Candies to People)

    这是小川的第393次更新,第425篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第256题(顺位题号是1103).我们通过以下方式向一排n = num_people个人分 ...

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

    [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...

随机推荐

  1. 吴裕雄--天生自然python机器学习:使用朴素贝叶斯过滤垃圾邮件

    使用朴素贝叶斯解决一些现实生活中 的问题时,需要先从文本内容得到字符串列表,然后生成词向量. 准备数据:切分文本 测试算法:使用朴素贝叶斯进行交叉验证 文件解析及完整的垃圾邮件测试函数 def cre ...

  2. StartDT AI Lab | 视觉智能引擎——Re-ID赋能线下场景顾客数字化

    人货场的思路是整个新零售数字化链路的核心,人是整个业务生命周期的起始点,图像算法的首要目标就是从图像中得到“人” .前一篇我们主要讲了Face ID的发展,Face ID帮助商家赋能了线下用户画像,把 ...

  3. 柱状图dataLabels 文字格式 以及如何获取柱子的name(名称)属性

    dataLabels: { formatter:funnctin(){ return this.percentage //只在堆叠图或饼图中有效,是该点相对总值的百分比. this.point //数 ...

  4. python中字典以key排序,以value排序。以及通过value找key的方式

    1.sorted函数首先介绍sorted函数,sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数. 其中iterable表示 ...

  5. SQL语言:DDL,DML,DCL,DQL,TCL

    DDL(Data Definition Language)数据库定义语言 statements are used to define the database structure or schema. ...

  6. Nginx笔记总结八:ngx_http_core_module模块中的变量

    $arg_patameter HTTP请求中某个参数的值,如/index.php?site=www.ttlsa.com,可以用$arg_site取得www.ttlsa.com这个值 $args HTT ...

  7. jdk源码理解-String类

    String类的理解 简记录一下对于jdk的学习,做一下记录,会持续补充,不断学习,加油 1.String的hash值的计算方法. hash值的计算方法多种多样,jdk中String的计算方法如下,比 ...

  8. Java Servlet XML文件配置

  9. Docker学习笔记_04 Rancher的部署安装(编排选用K8S)

    原文地址:http://dbase.cc/2018/01/12/docker/04_rancher的部署安装/ 为什么要使用Rancher Rancher是一个开源的企业级容器管理平台.通过Ranch ...

  10. Python 学习之Anaconda 设置默认打开chrome 浏览器

    笔者遇到的问题如何设置jupyter notebook 打开chrome 浏览器 1.打开anaconda prompt 2.输入jupyter notebook --generate-config ...