LeetCode 1103. Distribute Candies to People (分糖果 II)
题目标签: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)的更多相关文章
- [LeetCode] 1103. Distribute Candies to People 分糖果
题目: 思路: 本题一开始的思路就是按照流程一步步分下去,算是暴力方法,在官方题解中有利用等差数列进行计算的 这里只记录一下自己的暴力解题方式 只考虑每次分配的糖果数,分配的糖果数为1,2,3,4,5 ...
- LeetCode 1103. Distribute Candies to People
1103. Distribute Candies to People(分糖果||) 链接:https://leetcode-cn.com/problems/distribute-candies-to- ...
- 【Leetcode_easy】1103. Distribute Candies to People
problem 1103. Distribute Candies to People solution:没看明白代码... class Solution { public: vector<int ...
- LeetCode 575. Distribute Candies (发糖果)
Given an integer array with even length, where different numbers in this array represent different k ...
- 【leetcode】1103. Distribute Candies to People
题目如下: We distribute some number of candies, to a row of n = num_people people in the following way: ...
- LeetCode 575 Distribute Candies 解题报告
题目要求 Given an integer array with even length, where different numbers in this array represent differ ...
- LeetCode: 575 Distribute Candies(easy)
题目: Given an integer array with even length, where different numbers in this array represent differe ...
- LeetCode.1103-向人们分发糖果(Distribute Candies to People)
这是小川的第393次更新,第425篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第256题(顺位题号是1103).我们通过以下方式向一排n = num_people个人分 ...
- [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现
[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...
随机推荐
- Vue学习笔记【1】——什么是Vue.js
什么是Vue.js Vue.js 是目前最火的一个前端框架,React是最流行的一个前端框架(React除了开发网站,还可以开发手机App, Vue语法也是可以用于进行手机App开发的,需要借助于We ...
- SELinux导致PHP连接MySQL异常Can't connect to MySQL server的解决方法
原文摘自:http://www.jb51.net/article/52581.htm 这篇文章主要介绍了SELinux导致PHP连接MySQL异常Can't connect to MySQL serv ...
- (转)MySQL安装及配置指南
转:http://wiki.ubuntu.org.cn/MySQL%E5%AE%89%E8%A3%85%E6%8C%87%E5%8D%97 安装MySQL sudo apt-get install m ...
- 表单序列化+ajax跨域提交
php后台代码: use cmf\controller\HomeBaseController; use think\Db; header('Access-Control-Allow-Origin:*' ...
- git分布式版本控制系统权威指南学习笔记(二):git add暂存区的三个状态以及暂存区的理解
文章目录 不经过git add(到暂存区),能直接进行commit吗? 举个
- 20. Jmeter抓包之APP请求
APP测试过程中我们经常需要抓包,通常我们使用fiddler或者Charles.但是jmeter也可以抓包,而且非常好用,闲话不多说,下面进入正题. 步骤: 1.选择测试计划,添加线程组 2.选择工作 ...
- HBase启动错误提示别的机器60000已经存在
已经用cm 安装好了cdh,需要在上面添加HBase,并且做一个HBase故障转移功能,现在需要配置2个HMaster 在不同的机器上. 启动出现异常信息: 2015-12-23 14:44:38, ...
- leetcode.字符串.5最长回文子串-Java
1. 具体题目 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" ...
- linux下读取移动硬盘
前提是安装了ntfs-3g软件,系统才能识别到移动硬盘. 第一步.fdisk -l 该命令查看系统识别到的磁盘,如果移动硬盘系统能够识别, 在屏幕上会输出“/dev/sdb1”之类的字样. ...
- css 画饼图 倒计时圆圈
html <div class="pie"></div> css .pie{ width: 200px; height: 200px; border-rad ...