[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?
题意:给定无序的数列,保证大的比两侧中小的得到的糖果多,每个至少有一个
思路:正反遍历两次,正向遍历(从左往右)时,若后者比前者大,后者在前者的基础上加1;反向遍历,后者比前者小,前者加1。正向遍历一次,还是比较好想到的,那为什么要遍历两次,若整个数列是递减数列,则若只正向遍历一遍,所有得到的糖果都是1,这显然不符合题意,如4,3,2,1 ,其实只要开始时降序的就不行。那么方向遍历过程中,是不是只要前者比后者大就加1了?不是,我们举个反例“4,1,2,1”,正向遍历是,这四个小孩的得到的糖果数是:1,1,2,1,那么反向遍历时,若只要前者比后者大就加1会得到: 2,1,3,1,这显然不符合最少糖果的要求,最少应为2,1,2,1,这是因为正向遍历中,数组A[2]对应的元素值比两边都大,已经满足了其所得糖果比两边大的条件。总结一下:正向遍历,无法满足数组首元素为降序的情况;反向遍历,若只考虑前者比后者大,不满足若某元素已经取得局部最大的情况。所以此时应该加一定的限制条件,如代码所示:
class Solution {
public:
int candy(vector<int> &ratings)
{
int len=ratings.size();
int res=;
vector<int> cans(len,);
if(len<) return ;
for(int i=;i<len-;++i)
{
if(ratings[i]<ratings[i+])
cans[i+]=cans[i]+;
}
for(int i=len-;i>;i--)
{
if(ratings[i]<ratings[i-]&&cans[i-]<=cans[i]) //限定
cans[i-]=cans[i]+;
}
for(int i=;i<len;++i)
res+=cans[i];
return res;
}
};
还有一种空间复杂度为O(1)的解法,见GeekFans的博客,不过个人感觉思想类似,有兴趣可以看看。
[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 135]糖果分配 Candy
[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- leetcode — candy
/** * Source : https://oj.leetcode.com/problems/candy/ * * 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 @ 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分糖果
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
随机推荐
- 3D Food Printing【3D食物打印】
3D Food Printing There's new frontier in 3D printing that's begining to come into focus: food. 3D打印的 ...
- (数据科学学习手札21)sklearn.datasets常用功能详解
作为Python中经典的机器学习模块,sklearn围绕着机器学习提供了很多可直接调用的机器学习算法以及很多经典的数据集,本文就对sklearn中专门用来得到已有或自定义数据集的datasets模块进 ...
- Hibernate-ORM:12.Hibernate中的多对多关联关系
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客将讲述Hibernate中的多对多关联关系的操作,准备的篇幅较少,望海涵 一,讲述多对多 多对多的关联 ...
- onenet基础通信套件返回+CIS ERROR: 50的问题解决
1. 场景分析,主要问题就是有些操作返回+CIS ERROR: 50 2. 看了一下在AT+MIPLOBSERVERSP这个指令里面是没有返回+CIS ERROR: 50的错误类型的,所以应该是在解析 ...
- awk用法介绍
Awk 程序的结构如下: awk 'BEGIN{ print "start" } pattern { commands } END{ print "end" } ...
- Django笔记 —— 表单(form)
最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...
- Qt用委托绘制需要的图形的步骤
1.拷贝一份option: QStyleOptionViewItemV4 opt = option; 2.获取到widget,也是通过QStyleOptionViewItem &option ...
- 原生js实现五子棋
为什突然做这个,因为这是个笔试题,拖了一个月才写(最近终于闲了O(∩_∩)O),废话不多说,说说这个题吧 题目要求 编写一个单机[五子棋]游戏,要求如下: 1.使用原生技术实现,兼容 Chrome 浏 ...
- Python 3基础教程31-urllib模块
本文介绍Python里的urllib模块,这个urllib主要处理web服务的,如果需要做接口测试,或者写Python的网络爬虫,这个urllib就是最底层的库.需要用到里面的请求方法等. 1. 先看 ...
- 【app.json】配置说明,不断更新中
app.json文件用来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 tab 等. 注意: 1) json配置中键名.键值必须使用双引号,不能使用单引号. 2) 以 ...