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 ...
随机推荐
- centos6.5升级python为2.7
今天线上服务器全部升级python环境为python-2.7.6的环境,我采用的方法是ansible+shell,代码如下,友提,Python-2.7.6.tgz.setuptools-14.3.1. ...
- MySQL数据库命名及设计规范
1.设计原则 1) 标准化和规范化 数据的标准化有助于消除数据库中的数据冗余.标准化有好几种形式,但 Third Normal Form(3NF)通常被认为在性能.扩展性和数据完整性方面达到了最好平衡 ...
- 守护进程demon.c
1 #include <stdio.h> #include <unistd.h> #include <errno.h> #include <time.h> ...
- idea之internal java compiler error
启动错误:Error:java: Compilation failed: internal java compiler error 解决:将圈选地方改为对应的jdk版本即可
- Python自动化之django URL
URL url(r'^detail-(?P<nid>\d+)-(?P<uid>\d+).html', views.detail) 会把(?P\d+)和(?P\d+)传到后台 需 ...
- Flask 吐血400错误
的确地址或者method不匹配,这个容易解决 在后端中取到了form中不存在的元素! 这个很麻烦,特别form里面的信息比较多时!这个需要一一排查.另外取元素时最好用 request.form.get ...
- WebService -- Java 实现之 CXF (初体验)
1. 认识WebService 简而言之,她就是:一种跨编程语言以及操作系统的远程调用技术. 大家都可以根据定义好的规范和接口进行开发,尽管各自的使用的开发语言和操作系统有所不同,但是由于都遵循统一的 ...
- 【转】Spring mvc集成ZBUS--轻量级MQ、RPC、服务总线
本文转自:http://www.cnblogs.com/top15from/p/4899954.html ZBUS = MQ + RPC + PROXY 支持消息队列, 发布订阅, RPC, 代理(T ...
- PHP多进程编程实例
这篇文章主要介绍了PHP多进程编程实例,本文讲解的是在Linux下实现PHP多进程编程,需要的朋友可以参考下 羡慕火影忍者里鸣人的影分身么?没错,PHP程序是可以开动影分身的!想完成任务,又觉得一个进 ...
- Effective Python2 读书笔记2
Item 14: Prefer Exceptions to Returning None Functions that returns None to indicate special meaning ...