题目:


思路:

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

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

只考虑每次分配的糖果数,分配的糖果数为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. 36)PHP,搜寻数据库信息在html中显示(晋级1)

    首先是数据库的样子展示: 然后就是我的PHP主文件了: <?php class db { public $host="localhost" ;//这个是主机的地址 publi ...

  2. 吴裕雄--天生自然python学习笔记:python 用pygame模块制作一个音效播放器

    用 Sound 对象制作一个音效播放器. 应用程序总览 程序在执行后默认会把 WAV 音频文件加载到清单中,单击“播放”按钮可开始 播放,同时显示 “正在播放 xxx 音效”的信息 . 播放过程中,可 ...

  3. K3CLOUD表关联

    销售订单关联发货通知单 销售订单表 T_SAL_ORDER A T_SAL_ORDERENTRY B T_SAL_ORDERENTRY_LK C 发货通知单表 T_SAL_DELIVERYNOTICE ...

  4. 量化预测质量之分类报告 sklearn.metrics.classification_report

    classification_report的调用为:classification_report(y_true, y_pred, labels=None, target_names=None, samp ...

  5. numpy和pandas的基础索引切片

    Numpy的索引切片 索引 In [72]: arr = np.array([[[1,1,1],[2,2,2]],[[3,3,3],[4,4,4]]]) In [73]: arr Out[73]: a ...

  6. 从NIPS2014大会看机器学习新趋势

    微软杰出科学家 John Platt 本文译自:Machine Learning Trends fromNIPS 2014 编者按:John Platt是微软的杰出科学家,也是微软在机器学习领域的领军 ...

  7. 解决centos6系统上python3—flask模块的安装问题

    Flask 是一个使用 Python 编写的轻量级 Web 框架(所以我们前面花了那么多时间安装 Python3 呀).它被称为微型架构,因为其使用非常简单的核心以及功能丰富的扩展.虽然 Flask ...

  8. Go语言如何实现单例模式

    单例模式是常见的设计模式,被广泛用于创建数据库,redis等单实例.作用在于可以控制实例个数节省系统资源 特点: 保证调用多次,只会产生单个实例 全局访问 单例的分类 单例模式大致分为2大类: 懒汉式 ...

  9. jenkins使用(3)-设置定时任务

    配置时间如果错误,代码不会运行 *表示任意时刻

  10. jenkins使用(2)-配置项目代码的3种方式

    1.通过cmd命令直接进入项目代码的文件夹运行,注意路径中不要有中文 2.代码放到工作区:从本地复制项目代码到工作区目录下 代码结构的优化 3.代码连接git或svn,实时更新代码 svn检出 然后上 ...