Leetcode Candy
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
- Each child must have at least one candy.
- Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
题目的意思:
有N个孩子站成一条线,每个孩子分配一个排名。你将要给这些孩子一些糖果,要求:
- 每个孩子必须至少有一个糖果
- 孩子如果有个高排名,将得到糖果比其邻居的多
解题思路是:
(1)由于每个小孩至少有一个糖果,故将每个小孩的初始糖果初始化为1
(2)从前往后扫描,如果第i个小孩排名比第i-1高,那么第i个小孩的糖果数目+1
(3)从后往前扫描,如果第i个小孩排名比第i+1高,那么第i个小孩的糖果数目=max(第i个小孩的糖果数目,第i+1个小孩的糖果数目+1)
(4)最后将所有小孩的糖果数目累积即可
class Solution {
public:
int candy(vector<int> &ratings) {
vector<int> candy(ratings.size(),);
for(int i = ; i < ratings.size(); ++ i){
if(ratings[i]>ratings[i-]) candy[i]=candy[i-]+;
}
for(int i = ratings.size()-; i>=; -- i){
if(ratings[i] > ratings[i+]) candy[i] = max(candy[i+]+,candy[i]);
}
return accumulate(candy.begin(),candy.end(),);
}
};
关于此题目类似的题目
在一维数组中,找出一个点,使得其所有左边的数字均小于等于它,所有右边的数字都大于等于它。要求在线性时间内返回这个点所在的下标。
如A={1,0,1,0,1,2,3},返回下标4或5
解题思路与上面类似
首先,从左到右扫描一遍数组,通过一个辅助布尔数组记录哪些大于等于其之前所有元素的元素;
其次,从右到左扫描一遍数组,如果其后所有元素大于等于当前元素,而且在第一个遍历时当前元素大于等于之前的所有元素,则程序返回下标;
int getMagnitutePole(vector<int> A){
if(A.size() == ) return ;
vector<bool> flag(A.size(), false);
int curMax = A[];
for(int i = ; i < A.size(); ++ i){
if(A[i]>=curMax){
curMax = A[i];
flag[i] = true;
}
}
int curMin = A[A.size()-];
for(int i = A.size()-; i >=; -- i ){
if(A[i] <= curMin){
curMin = A[i];
if(flag[i]) return i;
}
}
return -;
}
Leetcode Candy的更多相关文章
- [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现
[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- [LeetCode] Candy Crush 糖果消消乐
This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...
- leetcode — candy
/** * Source : https://oj.leetcode.com/problems/candy/ * * There are N children standing in a line. ...
- [leetcode]Candy @ Python
原题地址:https://oj.leetcode.com/problems/candy/ 题意: There are N children standing in a line. Each child ...
- LeetCode: Candy 解题报告
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- [Leetcode] candy 糖果
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
随机推荐
- 图解c/c++多级指针与“多维”数组
声明:本文为原创博文,如有转载,请注明出处.若本文有编辑错误.概念错误或者逻辑错误,请予以指正,谢谢. 指针与数组是C/C++编程中非常重要的元素,同时也是较难以理解的.其中,多级指针与“多维”数组更 ...
- ecshop 导出exl表格
// 导出订单 if(isset($_POST['export'])){ // 统计金额 $sl = "SELECT SUM(goods_amount) as total from" ...
- 进入OS前的两步之System tick
OK,继续向操作系统迈进.由简入繁,先实现两个小功能.第一个是system tick,第二个是任务切换(PendSV).一个是操作系统的心跳,一个是操作系统的并发处理的具体实现. System tic ...
- Daily Scrum Meeting ——FirstDay
一.Daily Scrum Meeting照片 二.Burndown Chart 三.项目进展 1.介绍在github上发布的issues 根据MVC三层模型,我们主要发布的是V层以及M层,C层将根据 ...
- mongDB-- 3. 查询操作
1. 准备工作 (1)启动mongo 进入mongo安装目录的bin/ 目录 , ./mongod (2)启动mongo客户端 ./mongo (3) 查看所有库 show dbs; (4) 切换到l ...
- 如何给外部引用的js文件传递参数
1.定义全局变量 <script language="javascript"> var g = "I'm here"; </script> ...
- 各大浏览器hack
浏览器界的奇葩IE 样式前缀法,在需要制定某些浏览器只识别其中某些属性的时候: chrome/safari 都是webkit内核的浏览器.在属性前加前缀-webkti- firefox 只在火狐浏览器 ...
- Java的内存机制
Java 把内存划分成两种:一种是栈内存,另一种是堆内存.在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配,当在一段代码块定义一个变量时,Java 就在栈中为这个变量分配内存空 ...
- Servlet.init() for servlet springMvc
- celery 异步任务小记
这里有一篇写的不错的:http://www.jianshu.com/p/1840035cb510 自己的"格式化"后的内容备忘下: 我们总在说c10k的问题, 也做了不少优化, 然 ...