[LeetCode] 1103. Distribute Candies to People 分糖果
题目:
思路:
本题一开始的思路就是按照流程一步步分下去,算是暴力方法,在官方题解中有利用等差数列进行计算的
这里只记录一下自己的暴力解题方式
只考虑每次分配的糖果数,分配的糖果数为1,2,3,4,5,..., 依次加1
再考虑到分配的轮数,可以利用 i % num_people 来求得第i次应该分配到第几个人
最后要注意的是,如果当前糖果数小于本应该分配的糖果数,则将当前糖果全部给予,也就是要判断剩余糖果数 candies 与本该分配糖果数 i+ 的大小,谁小分配谁
代码:
- class Solution {
- public:
- vector<int> distributeCandies(int candies, int num_people) {
- // vector<int> res(num_people, 0);
- // int i=1;
- // for(int k=0; candies>=i+k*num_people; k++){
- // while(i <= num_people){
- // if(candies<i+k*num_people){
- // res[i-1] += candies;
- // candies = 0;
- // break;
- // }
- // res[i-1] += i+k*num_people;
- // candies -= i+k*num_people;
- // i++;
- // }
- // i=1;
- // }
- // if(candies) res[0] += candies;
- // return res;
- vector<int> ans(num_people); // 初始元素为0
- int i=;
- while(candies!=){
- ans[i % num_people] += min(candies, i+);
- candies -= min(candies, i+);
- i++;
- }
- return ans;
- }
- };
被注释的代码是自己的想法,虽然复杂度相同,看到题解才发现自己小题大做,没有理解透题目
[LeetCode] 1103. Distribute Candies to People 分糖果的更多相关文章
- LeetCode 1103. Distribute Candies to People (分糖果 II)
题目标签:Math 题目让我们分发糖果,分的糖果从1 开始依次增加,直到分完. for loop可以计数糖果的数量,直到糖果发完.但是还是要遍历array 给people 发糖,这里要用到 index ...
- 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. ...
随机推荐
- replace|同时替换
a= 'eeekkksksksk' print a.replace('e','s').replace('s','k') #kkkkkkkkkkkk change={"e":&quo ...
- 三角插值的 Fourier 系数推导
给定 $k$ 个互不相同的复数 $x_0,\cdots,x_{k-1}$,以及 $k$ 个复数$y_0,\cdots,y_{k-1}$.我们知道存在唯一的复系数 $k-1$ 次多项式$$\mathca ...
- [LC] 102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- python-django-linux上mysql的安装和配置_20191124
又有了阻塞了,怎么在Linux创建数据库,mysql, 我把数据库安装在Linux上, 1,sudo apt-get install mysql-server 2,ps -aux | grep 'my ...
- youths |government|some
N-COUNT (新闻用语,尤指惹麻烦的)青年,小伙子Journalists often refer to young men as youths, especially when they are ...
- Android下的鉴权实现方案
软件原理 不赘述,参考: 软件License认证方案的设计思路 License文件离线鉴权 机械指纹,不可逆的加密算法,如MD5 功能鉴权,可逆的不对称加密算法,服务端公钥加密,app端私钥解密,如R ...
- perf4j @Profiled常用写法
以下内容大部分摘抄自网络上信息. 1.默认写法 @Profiled 日志语句形如: 2009-09-07 14:37:23,734 [main] INFO org.perf4j.TimingLogge ...
- python初认函数
今日所得 函数基本使用 函数的参数 函数的返回值 # 函数内要想返回给调用者值 必须用关键字return """ 不写return 只写return 写return No ...
- mysql 优化配置和方面
MySQL性能优化的参数简介 公司网站访问量越来越大,MySQL自然成为瓶颈,因此最近我一直在研究 MySQL 的优化,第一步自然想到的是 MySQL 系统参数的优化,作为一个访问量很大的网站(日20 ...
- linux kill进程没有立刻停止
前些天在执行restart脚本的时候遇到了一个奇怪的问题:1.第一次执行进程不见了,启动失败2.第二次重启进程成功,但是在kill的时候提示进程不存在需要重启两次进程才能成功 查看日志文件:第一次重启 ...