题目标签: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. 8、iota枚举

    1.iota常量自动生成器,每一行,自动累加1 2.iota给常量赋值使用 3.如果iota遇到const,就会重置为0 4.可以可以只写一个iota 5.如果是同一行,值是一样的 // 09_iot ...

  2. GERALD07加强版题解

    题目描述: N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. 输入格式: 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密. 接下来M行,代表图 ...

  3. NX二次开发-OLE/COM向EXCEL表格中插入图片

    今晚有一个兄弟问我怎么往EXCEL里插入图片(加工程序单中需要插入图片),这个我之前也没弄过,回复了他一句不知道,后来刚刚干完游戏吃完鸡,就去VC++的书上翻了翻,还真的被我翻到了.VC++的方法往E ...

  4. 关于“Unknown or unsupported command 'install'”问题解决的小结

    经常需要在COMMAND命令中安装第三方库,有时会碰到“Unknown or unsupported command 'install'”这种报错. 刚开始时,以为是环境变量里面没有配置:PYTHON ...

  5. Hibernate 和 JPA 注解

    转载请注明:Hibernate 和 JPA 注解 | 言曌博客 1.@Entity(name="EntityName") 必须, name为可选,对应数据库中一的个表 2.@Tab ...

  6. 与DSP通信时,RD&WR信号

      /////////////////////////////////////////////////////////// :] rd,wr; :] dsp_data_out; 'hzzzz; // ...

  7. Spring 学习笔记 IoC 基础

    Spring IoC Ioc 是什么 IoC -- Inversion of Control(控制反转)什么是控制?什么是反转? 控制反转了什么? 在很早之前写项目不用 Spring 的时候,都是在 ...

  8. 依赖Anaconda环境安装TensorFlow库,避免采坑

    TensorFlow™ 简介: TensorFlow是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edges)则表示 ...

  9. LitJson使用中的一些问题

    http://blog.csdn.net/n5/article/details/45030063

  10. zip压缩详细分析

    该文章转自:http://www.cnblogs.com/esingchan/p/3958962.html (文章写得很详细,让我对zip压缩有了了解,感谢博主,贴在这是为了防止忘了有这么好的文章,侵 ...