题目:


思路:

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

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

只考虑每次分配的糖果数,分配的糖果数为1,2,3,4,5,..., 依次加1

再考虑到分配的轮数,可以利用 i % num_people 来求得第i次应该分配到第几个人

最后要注意的是,如果当前糖果数小于本应该分配的糖果数,则将当前糖果全部给予,也就是要判断剩余糖果数 candies 与本该分配糖果数 i+ 的大小,谁小分配谁

代码:

  1. class Solution {
  2. public:
  3. vector<int> distributeCandies(int candies, int num_people) {
  4. // vector<int> res(num_people, 0);
  5. // int i=1;
  6. // for(int k=0; candies>=i+k*num_people; k++){
  7. // while(i <= num_people){
  8. // if(candies<i+k*num_people){
  9. // res[i-1] += candies;
  10. // candies = 0;
  11. // break;
  12. // }
  13. // res[i-1] += i+k*num_people;
  14. // candies -= i+k*num_people;
  15. // i++;
  16. // }
  17. // i=1;
  18. // }
  19. // if(candies) res[0] += candies;
  20. // return res;
  21. vector<int> ans(num_people); // 初始元素为0
  22. int i=;
  23. while(candies!=){
  24. ans[i % num_people] += min(candies, i+);
  25. candies -= min(candies, i+);
  26. i++;
  27. }
  28. return ans;
  29. }
  30. };

被注释的代码是自己的想法,虽然复杂度相同,看到题解才发现自己小题大做,没有理解透题目

[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. replace|同时替换

    a= 'eeekkksksksk' print a.replace('e','s').replace('s','k') #kkkkkkkkkkkk change={"e":&quo ...

  2. 三角插值的 Fourier 系数推导

    给定 $k$ 个互不相同的复数 $x_0,\cdots,x_{k-1}$,以及 $k$ 个复数$y_0,\cdots,y_{k-1}$.我们知道存在唯一的复系数 $k-1$ 次多项式$$\mathca ...

  3. [LC] 102. Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  4. python-django-linux上mysql的安装和配置_20191124

    又有了阻塞了,怎么在Linux创建数据库,mysql, 我把数据库安装在Linux上, 1,sudo apt-get install mysql-server 2,ps -aux | grep 'my ...

  5. youths |government|some

    N-COUNT (新闻用语,尤指惹麻烦的)青年,小伙子Journalists often refer to young men as youths, especially when they are ...

  6. Android下的鉴权实现方案

    软件原理 不赘述,参考: 软件License认证方案的设计思路 License文件离线鉴权 机械指纹,不可逆的加密算法,如MD5 功能鉴权,可逆的不对称加密算法,服务端公钥加密,app端私钥解密,如R ...

  7. perf4j @Profiled常用写法

    以下内容大部分摘抄自网络上信息. 1.默认写法 @Profiled 日志语句形如: 2009-09-07 14:37:23,734 [main] INFO org.perf4j.TimingLogge ...

  8. python初认函数

    今日所得 函数基本使用 函数的参数 函数的返回值 # 函数内要想返回给调用者值 必须用关键字return """ 不写return 只写return 写return No ...

  9. mysql 优化配置和方面

    MySQL性能优化的参数简介 公司网站访问量越来越大,MySQL自然成为瓶颈,因此最近我一直在研究 MySQL 的优化,第一步自然想到的是 MySQL 系统参数的优化,作为一个访问量很大的网站(日20 ...

  10. linux kill进程没有立刻停止

    前些天在执行restart脚本的时候遇到了一个奇怪的问题:1.第一次执行进程不见了,启动失败2.第二次重启进程成功,但是在kill的时候提示进程不存在需要重启两次进程才能成功 查看日志文件:第一次重启 ...