[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 ...
随机推荐
- JavaScript鼠标事件
mousedown 鼠标被按下. mouseup 鼠标按钮被释放(抬起). click 鼠标左键(或中建)被单击. 事件触发顺序:mousedown>mouseup>click>db ...
- thinkphp phpmailer邮箱验证
thinkphp 关于phpmailer的邮箱验证 一 . 登陆自己的邮箱,例如:qq邮箱.登陆qq邮箱在账户设置中开启smtp服务: 之后回发送一个授权码 , 这个授权码先保存下来,这个授权码在后 ...
- scala成长之路(3)隐式转换
不废话,先上例子:定义一个参数类型为String的函数: scala> def sayHello(name:String) = println("hello " + name ...
- Java学习笔记三:Java的变量、常量、变量的类型及使用规则
Java的变量.常量.变量的类型及使用规则 每一种语言都有一些具有特殊用途的词,Java也不例外,它们被称为关键字.关键字对 Java 的编译器有着特殊的意义. 一:Java中的关键字: 注:Java ...
- UVa Problem 100 The 3n+1 problem (3n+1 问题)
参考:https://blog.csdn.net/metaphysis/article/details/6431937 #include <iostream> #include <c ...
- 基于Ubuntu Server 16.04 LTS版本安装和部署Django之(五):测试项目
基于Ubuntu Server 16.04 LTS版本安装和部署Django之(一):安装Python3-pip和Django 基于Ubuntu Server 16.04 LTS版本安装和部署Djan ...
- linux命令大全(转载)
在搭建openstack时遇到问题,导致上网查询相关信息.找到一篇不错的文章,希望对大家有用.下附地址: http://blog.csdn.net/junbujianwpl/article/detai ...
- SQL On Streaming
此文已由作者岳猛授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 实时计算的一个方向 实时计算未来会成为一个趋势,基本上所有的离线计算任务都能通过实时计算来完成,对于实时计算来 ...
- Java并发(多线程)
一.多线程的基本概念 1.什么是进程.多进程有什么作用? 大家都使用计算机,当我们打开某一个软件的时候,其实就是启动了一个进程,可以打开任务管理器看看,我们打开的每一个软件,都是一个进程,在同一个操作 ...
- selenium自动化登录qq网页
一个简单的登录网页上qq的脚本,通过此脚本了解到有些位置是无法通过xpath来定位的反倒是By定位更方便 #encoding=utf-8 from selenium import webdriver ...