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. ...
随机推荐
- springCloud参考资料
官网: http://spring.io/projects/spring-cloud 各组件说明(中文版):https://springcloud.cc/spring-cloud-netflflix. ...
- 每天一个Linux命令:mkdir(4)
mkdir mkdir命令 用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录 格式 mkdir [选项] [目录..] 参数选项 参数 备 ...
- 树————N叉树的层序遍历
思想: 使用队的思想,将每一层的节点放入队列中,依次弹出,同时将其children放入队列. c++ /* // Definition for a Node. class Node { public: ...
- Hbase的读写流程
HBase读写流程 1.HBase读数据流程 HRegionServer保存着meta表以及表数据,要访问表数据,首先Client先去访问zookeeper,从zookeeper里面获取meta表所在 ...
- XSS的原理分析与解剖(第二篇)
0×01 前言: 上节(http://www.freebuf.com/articles/web/40520.html)已经说明了xss的原理及不同环境的构造方法.本期来说说XSS的分类及挖掘方法. 当 ...
- ionic学习使用笔记(一) 版本更新及创建项目时遇到的问题解决
最近开始用ionic开发项目,虽然去年的时候用ionic 2.0 开发过公司的项目,不过现在的ionic已经升级到了ionic framework 3.0 了.而且还有个 ionic-cli . 使用 ...
- web前端Vue+Django rest framework 框架 生鲜电商项目实战✍✍✍
web前端Vue+Django rest framework 框架 生鲜电商项目实战 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频 ...
- pytest--fixture之参数化
场景:测试用例执行时,有的用例需要登陆才能执行,有些用例 不需要登陆.setup和teardown无法满足.fixture可以.默认 scope(范围)function • 步骤: 1. 导入pyte ...
- Vuex 源码解析
先来看一下这张Vuex的数据流程图,熟悉Vuex使用的同学应该已经有所了解. Vuex实现了一个单向数据流,在全局拥有一个State存放数据,所有修改State的操作必须通过Mutation进行,Mu ...
- python--面向对象:多态与封装
一.多态 :python 天生支持多态多态指的是一类事物有多种形态 eg:文件有多种形态:文本文件,可执行文件鸭子类型:python中崇尚鸭子类型,不崇尚根据继承所得来的相似 优点 : 松耦合 每个相 ...