1103. Distribute Candies to People(分糖果||)

链接:https://leetcode-cn.com/problems/distribute-candies-to-people/

题目:

  排排坐,分糖果。

  我们买了一些糖果 candies,打算把它们分给排好队的 n = num_people 个小朋友。

  给第一个小朋友 1 颗糖果,第二个小朋友 2 颗,依此类推,直到给最后一个小朋友 n 颗糖果。

  然后,我们再回到队伍的起点,给第一个小朋友 n + 1 颗糖果,第二个小朋友 n + 2 颗,依此类推,直到给最后一个小朋友 2 * n 颗糖果。

  重复上述过程(每次都比上一次多给出一颗糖果,当到达队伍终点后再次从队伍起点开始),直到我们分完所有的糖果。注意,就算我们手中的剩下糖果数不够(不比前一次发出的糖果多),这些糖果也会全部发给当前的小朋友。

  返回一个长度为 num_people、元素之和为 candies 的数组,以表示糖果的最终分发情况(即 ans[i] 表示第 i 个小朋友分到的糖果数)。

  示例 1:

  输入:candies = 7, num_people = 4
  输出:[1,2,3,1]
  解释:
  第一次,ans[0] += 1,数组变为 [1,0,0,0]。
  第二次,ans[1] += 2,数组变为 [1,2,0,0]。
  第三次,ans[2] += 3,数组变为 [1,2,3,0]。
  第四次,ans[3] += 1(因为此时只剩下 1 颗糖果),最终数组变为 [1,2,3,1]。
  示例 2:

  输入:candies = 10, num_people = 3
  输出:[5,2,3]
  解释:
  第一次,ans[0] += 1,数组变为 [1,0,0]。
  第二次,ans[1] += 2,数组变为 [1,2,0]。
  第三次,ans[2] += 3,数组变为 [1,2,3]。
  第四次,ans[0] += 4,最终数组变为 [5,2,3]。

  提示:

  1 <= candies <= 10^9

  1 <= num_people <= 1000

思路:

  没啥思路,套一个循环,注意改变数量和及时跳出即可。如果想要更简单一点的,可以通过通项公式直接算出该轮所需糖果,直接分配也行。

代码:

 public int[] distributeCandies(int candies, int num_people) {
int[] number = new int[num_people];
int num = 1;
int i = 0;
while (candies > 0) { number[i] += num;
candies -= num;
i++;
if (i == num_people) {
i = 0;
}
if (candies > num + 1) {
num++;
} else {
number[i] += candies;
break;
} }
return number;
}

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 分糖果

    题目: 思路: 本题一开始的思路就是按照流程一步步分下去,算是暴力方法,在官方题解中有利用等差数列进行计算的 这里只记录一下自己的暴力解题方式 只考虑每次分配的糖果数,分配的糖果数为1,2,3,4,5 ...

  3. 【Leetcode_easy】1103. Distribute Candies to People

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

  4. 【leetcode】1103. Distribute Candies to People

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

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

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

  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] Distribute Candies 分糖果

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

  9. LeetCode Distribute Candies

    原题链接在这里:https://leetcode.com/problems/distribute-candies/#/description 题目: Given an integer array wi ...

随机推荐

  1. what should I use .post vs .ajax?

    what should I use .post vs .ajax? 问题: I've always had this dilemma困境 whether to use .post or .ajax f ...

  2. 原生JavaScript实现函数的防抖和节流

    原生JavaScript实现函数的防抖和节流 参考:https://www.jianshu.com/p/c8b86b09daf0 想详细了解的直接戳上面链接了,讲得非常清楚.下面只给代码和我自己写的注 ...

  3. 多网卡下如何配置指定IP走某个路由器(适用于外网不通,但是钉钉服务器通的情况)

    # 多网卡下如何配置指定IP走某个路由器(适用于外网不通,但是钉钉服务器通的情况) ## 如何查看一个进程建立的网络连接,方式一 - 查看进程pid `ps -ef|grep dingtalk`- 查 ...

  4. Request模块入门学习

    使用指令npm install --save request来安装模块,然后使用var request = require('request')完成引用. 对于GET请求,主要是获取目的url中数据. ...

  5. Angular 执行 css3 简单的动画

    <div class="content"> 内容区域 <button (click)="showAside()">弹出侧边栏</b ...

  6. [Java复习] 缓存Cache part2

    7. Redis持久化有哪几种方式?不同的持久化机制都有什么优缺点?持久化机制具体底层是如何实现的? 为什么要持久化? 如果只是存在内存里,如果redis宕机再重启,内存数据就丢失了,所以要用持久化机 ...

  7. 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_17-课程预览功能开发-前后端测试

    启动前端代码 前端课程找到课程的发布页面 这样就打开了预览页面 结束

  8. QML访问C++类内部

    0.前提 C++类需要继承QObject,且有Q_OBJECT宏[所以QT自己的那么多类按道理上QML都是可以直接访问的,nb] 1.QML访问C++中的非private槽函数 可直接调用 信号也是 ...

  9. 123457123456#0#-----com.threeapp.headsoccer----宝宝头球大战

    com.threeapp.headsoccer----宝宝头球大战

  10. Docker 容器资源隔离 namespace(十)

    目录 一.简介 Linux Namespace的6大类型 二.Mount Namespace 三.IPC Namespace 四.Network Namespace 五.UTS Namespace 六 ...