题目标签:Math

  题目让我们分发糖果,分的糖果从1 开始依次增加,直到分完。

  for loop可以计数糖果的数量,直到糖果发完。但是还是要遍历array 给people 发糖,这里要用到 index = (本轮分糖果的量 % people 的人数)糖果的数量从0 开始计数,这样的话,index 就会一直重复遍历 array,具体看code。

Java Solution:

Runtime:  1ms, faster than 90.53%

Memory Usage: 33.8 MB, less than 100.00%

完成日期:07/15/2019

关键点:利用%重复遍历array

class Solution {
public int[] distributeCandies(int candies, int num_people) {
int[] people = new int[num_people]; for(int give = 0; candies > 0; candies -= give) {
people[give % num_people] += Math.min(candies, ++give);
} return people;
}
}

参考资料:LeetCode discuss

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 1103. Distribute Candies to People (分糖果 II)的更多相关文章

  1. [LeetCode] 1103. Distribute Candies to People 分糖果

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

  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. rails问题检查

    在做rails postgresql过程中,使用同事的老代码,然后坐到db:migrate时发现总是报错 这种错误是无法在百度或者google搞定的,需要去进入到底层查看 所以需要进入相关文件看,到d ...

  2. Delphi Base64编码_解码及ZLib压缩_解压(转)

    最近在写的程序与SOAP相关,所以用到了一些Base64编码/解码及数据压缩/解压方面的知识. 在这里来作一些总结:一.Base64编码/解码 一般用到的是Delphi自带的单元EncdDecd,当然 ...

  3. 用C语言实现yield

    用C语言实现yield C/C++中没有yield语法,有的时候想用产生器,自己实现一个循环会感觉很麻烦.C/C++应该如何实现产生器呢? class FibonacciGenerator { pub ...

  4. 二叉树总结及部分Lintcode题目分析 1

    1. 遍历问题 Preorder / Inorder / Postorder preorder: root left right inorder: left root right postorder: ...

  5. Karaf基础知识

    Karaf 遵循OSGi开发规范的一个Apache框架 1.命令形如:scope:name   举例:feature:list 2 shell:completion tab键补齐 GLOBAL 补齐显 ...

  6. msgbox用法

    http://www.w3school.com.cn/vbscript/func_msgbox.asp VBScript MsgBox 函数 VBScript 函数参考手册 定义和用法 MsgBox ...

  7. java 8 bug

    jpa保存实体的时候,不能用{{}}初始化对象,否则会报异常 org.springframework.dao.InvalidDataAccessApiUsageException: Unknown e ...

  8. LCA的RMQ求法

    参考博客 仔细想一想:最近的公共祖先,其实,搜索时回朔,连通这两点,那深度最低肯定是最近的公共祖先啊. 那这样就可以变成RMQ问题了. #include<stdio.h> #include ...

  9. hdu6311 /// 欧拉路径 无向图最小路径覆盖 输出正反路径

    题目大意: 给定n m 为图的点数和边数 接下来m行 u v 为u到v有一条边 要求最少几笔能画完图的所有边 输出每笔画过的路径编号 正数编号正向 负数编号反向 题解:https://www.cnbl ...

  10. 数据可视化echart

    刚接到这个一脸懵逼,到现在还算有点眉目,先上个图庆祝一下,哈哈,开心.... 最近没时间,等待更新..........