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. 伍德伯里矩阵恒等式(Woodbury matrix identity)

    宜言饮酒,与子偕老.琴瑟在御,莫不静好. 更多精彩内容请关注微信公众号 "优化与算法" 在数学(特别是线性代数)中,Woodbury矩阵恒等式是以Max A.Woodbury命名的 ...

  2. Interacted Action-Driven Visual Tracking Algorithm

    文章来源:Attentional Action-Driven Deep Network for Visual Object Tracking   博士论文(2017年8月份完稿) http://s-s ...

  3. Matrix: android 中的Matrix (android.graphics.Matrix) (转)

    本篇博客主要讲解一下如何处理对一个Bitmap对象进行处理,包括:缩放.旋转.位移.倾斜等.在最后将以一个简单的Demo来演示图片特效的变换. 1. Matrix概述 对于一个图片变换的处理,需要Ma ...

  4. MacOS系统降级

    从MacOS 10.14 降级到 10.12,下载好系统镜像文件.打开,复制到Application. 准备一个至少8G的U盘,,打开磁盘工具,『抹掉』(格式化)成Mac OS扩展(日志式),名称可随 ...

  5. 复习Android布局

    效果如图: 这里没有做逻辑的处理,仅仅是布局的罗列.包括垂直和水平的线性布局,以及一个滚动的view. <?xml version="1.0" encoding=" ...

  6. RabbitMQ 3.7.X集群:从入门到精通,这一篇就够了

    RabbitMQ是流行的开源消息队列系统,本身已经具备了较强的并发处理速度及运行稳定性,然而在大规模的实际应用中,往往还需要使用集群配置来保证系统中消息通信部分的高可用性,并发处理性能及异常恢复能力. ...

  7. HTML5Audio/Video全解(疑难杂症)

    1.mp4格式视频无法在chrome中播放 Chrome浏览器支持HTML5,它支持原生播放部分的MP4格式(不用通过Flash等插件).为 什么是部分MP4呢?MP4有非常复杂的含义(见http:/ ...

  8. 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_05-Feign远程调用-客户端负载均衡介绍

    2 Feign远程调用 在前后端分离架构中,服务层被拆分成了很多的微服务,服务与服务之间难免发生交互,比如:课程发布需要调用 CMS服务生成课程静态化页面,本节研究微服务远程调用所使用的技术. 下图是 ...

  9. 简单模拟CO模块

    promise方式: // 对co模块的简单模拟 function run(gen){ var g = gen(); function next(data){ var result = g.next( ...

  10. springboot拦截json后缀的请求,返回json数据

    需求:请求list.json返回以下数据 { "jsonResult": { "code": 200, "message": "查 ...