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. ...
随机推荐
- sql 性能优化 索引碎片
1.索引 简单的说,索引就像书本的目录,目录可以快速找到所在页数,数据库中索引可以帮助快速找到数据,而不用全表扫描,合适的索引可以大大提高数据库查询的效率.(1). 优点大大加快了数据库检索的速度,包 ...
- hdu1848 Fibonacci again and again [组合游戏]
http://acm.hdu.edu.cn/showproblem.php?pid=1848 Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers) ...
- 构造——cf1213E
分情况讨论,构造很简单 #include<bits/stdc++.h> using namespace std; #define N 200005 ],t[]; int n,s1,s2,t ...
- ionic学习使用笔记(一) 版本更新及创建项目时遇到的问题解决
最近开始用ionic开发项目,虽然去年的时候用ionic 2.0 开发过公司的项目,不过现在的ionic已经升级到了ionic framework 3.0 了.而且还有个 ionic-cli . 使用 ...
- RocktMq安装和简单使用以及报错收集
文章目录 安装 使用 报错 总结: rocketmq内存设置 配置brockerip 启动方式 如果往机器上部署,最好再本地看看报错吗 关于防火墙 看总结去吧 安装 准备: jdk1.8 maven ...
- JAVA求回文数
Manacher算法(马拉车算法)时间复杂度O(n) 用过中心检测法(就是上面说的O(n2) O(n^2)O(n )的算法)的都知道对于奇数回文串和偶数回文串的处理是不同的,奇数回文串有2n+1 2n ...
- 用js onselectstart事件鼠标禁止选中文字
禁止鼠标选中文本,针对不同浏览器有不同的写法.常见的是onselectstart表示禁止鼠标选中文本.其中用js可以在页面中写 onselectstart="return false&quo ...
- linux模范配置文件格式
模范配置文件 #--------------------------------------------------------------------- # Global settings #--- ...
- UI自动化ADB出现devices offline的解决方法
终端运行如下命令即可解决: adb kill-server adb start-server adb remount
- java-day20
注解:说明程序的,给计算机看的 注释:用文字描述程序的,给程序员看的 定义:注解(Annotation),也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性.与类.接口.枚举是在 ...