[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. ...
随机推荐
- C2. Power Transmission (Hard Edition)(线段相交)
This problem is same as the previous one, but has larger constraints. It was a Sunday morning when t ...
- D - Association for Control Over Minds Kattis - control (并查集+STL)
You are the boss of ACM (Association for Control over Minds), an upstanding company with a single go ...
- day45-多线程(server和多个client通信)
#server: import socket from threading import Thread def func(conn): conn.send(b'hello client') ret = ...
- SpringBoot1.X /2.X使用@Component注解注入为java.lang.NullPointException 问题小结
问题简述: 我们在使用Spring系的产品的时候总是想当然的以为所有指定的Bean都会让Spring来管理,其实不然,即使是Spring出品的产品也不全是的哦,以下是我遇到的一个使用@Componen ...
- django框架进阶-使用缓存-长期维护
############### django-缓存页面 ############### ########################################### # 全站缓存, ...
- 从程序到系统:建立一个更智能的世界——记Joseph Sifakis“21世纪的计算”大会主题演讲
Sifakis"21世纪的计算"大会主题演讲" title="从程序到系统:建立一个更智能的世界--记Joseph Sifakis"21世纪的计算&q ...
- left join on和where 限制查询的区别在于
left join on: 会显示前表的所有数据,不满足显示为null或者为0 . 而where显示的为满足条件的记录,不满足但是存在的数据不显示. 做统计数据的时候,用join on比较合理.
- SpringMVC学习笔记五:HandlerExceptionResolver异常处理
继承HandlerExceptionResolver自定义异常处理器 控制器ExceptionController.java package com.orange.controller; import ...
- JS Style Guide_1
当你在回调函数里要使用函数表达式时,尽量使用箭头函数,比如数组中的 Map.filter.reduce等的回调函数中 [1,2,3].map((x) => { let y = x + 1; re ...
- JAVA WEB期末项目第二阶段成果
我们做的系统是一个基于Java web与MySQL的食堂订餐系统 班级: 计科二班 小组成员:李鉴宣.袁超 1.开发环境 开发编辑器使用:Visual Studio Code 数据库使用:MySQL8 ...
