题目:


思路:

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

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

只考虑每次分配的糖果数,分配的糖果数为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. 46)PHP,PHP语言为啥需要服务器

    1)用户的 Web 浏览器发出 HTTP 请求,请求特定 Web 页面. 2)Web服务器收到.php 的请求获取该文件,并将它传到 PHP 引擎,要求它处理. 3)PHP 引擎开始解析脚本. 脚本中 ...

  2. D. Welfare State

    There is a country with 

  3. B. Split a Number(字符串加法)

    Dima worked all day and wrote down on a long paper strip his favorite number nn consisting of ll dig ...

  4. day12-模块导入

    # 一.import import demo # in demo.py -- 导入demo模块,执行里面的print语句. print(demo.money) # 8000000 -- 打印demo的 ...

  5. Exchange Online 权限管理

    在Exchange管理中心,通过权限管理可为管理员.普通用户以及Outlook Web App分别制定不同的权限和策略,以满足精细化分工或差异化角色的需要. 一.管理角色组 组织管理者使用角色组来向管 ...

  6. 浏览器加载、渲染html的顺序和页面优化

    浏览器加载和渲染html的顺序 1. IE下载的顺序是从上到下,渲染(就是把请求的内容显示到浏览器屏幕上)的顺序也是从上到下,下载和渲染是同时进行的. 2. 在渲染到页面的某一部分时,其上面的所有部分 ...

  7. OpenCV 特征点检测

    #include <stdio.h> #include <iostream> #include "opencv2/core/core.hpp" #inclu ...

  8. SpringBoot webjars 映射

    添加静态资源映射 @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.a ...

  9. Mac系统常用软件

    1.往移动硬盘中拷贝东西.创建文件夹: Mounty 2.解压缩软件(rar): the unarchiver 3.类似Xshell软件: FinalShell(国产) 输入以下命令: curl -o ...

  10. Welcome to Fan Ouyang’s website!

    Welcome to Fan Ouyang's website! 欧阳璠,哲学博士,湖南娄底人. 目前为浙江大学教育学院课程与学习科学系教育技术专业百人计划研究员. 2013-2018年 明尼苏达大学 ...