本文senlie原版的,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Candy

Total Accepted: 16494 Total
Submissions: 87468My Submissions

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)每一个小孩至少一颗糖

2)评分高的小孩发的糖比他旁边两个小孩的多

思路:左右 dp

用一个数组 candy[n],表示第 i 个小孩所应该发的最少糖果数

数组 ratings[n] 表示每一个小孩的评分

1.从左到右扫描一遍, candy[i] = 1, if ratings[i] <= ratings[i-1] ; candy[i] = candy[i-1] + 1, if ratings[i] > ratings[i-1]

2.从右到左扫描一遍, candy[i] = candy[i], if ratings[i] <= ratings[i+1] ; candy[i] = max(candy[i], candy[i+1] + 1), if ratings[i] > ratings[i+1]

3.accumulate(candy, candy + n, 0)



复杂度: 时间 O(n), 空间 O(n)

int candy(vector<int> &ratings){
int n = ratings.size();
vector<int> candy(n, 1);
for(int i = 1; i < n; ++i){
candy[i] = ratings[i] <= ratings[i - 1] ? 1 : candy[i - 1] + 1;
}
for(int i = n - 2; i > -1;--i){
candy[i] = ratings[i] <= ratings[i + 1] ? candy[i] : max(candy[i], candy[i + 1] + 1);
}
return accumulate(candy.begin(), candy.end(), 0);
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

Leetcode 动态规划 Candy的更多相关文章

  1. 快速上手leetcode动态规划题

    快速上手leetcode动态规划题 我现在是初学的状态,在此来记录我的刷题过程,便于以后复习巩固. 我leetcode从动态规划开始刷,语言用的java. 一.了解动态规划 我上网查了一下动态规划,了 ...

  2. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  3. (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重

    原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...

  4. [LeetCode] 动态规划入门题目

    最近接触了动态规划这个厉害的方法,还在慢慢地试着去了解这种思想,因此就在LeetCode上面找了几道比较简单的题目练了练手. 首先,动态规划是什么呢?很多人认为把它称作一种"算法" ...

  5. LeetCode 动态规划

    动态规划:适用于子问题不是独立的情况,也就是各子问题包含子子问题,若用分治算法,则会做很多不必要的工作,重复的求解子问题,动态规划对每个子子问题,只求解一次将其结果保存在一张表中,从而避免重复计算. ...

  6. [LeetCode] 723. Candy Crush 糖果消消乐

    This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...

  7. LeetCode 723. Candy Crush

    原题链接在这里:https://leetcode.com/problems/candy-crush/ 题目: This question is about implementing a basic e ...

  8. [LeetCode] 723. Candy Crush 糖果粉碎

    This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...

  9. LeetCode动态规划题总结【持续更新】

    以下题号均为LeetCode题号,便于查看原题. 10. Regular Expression Matching 题意:实现字符串的正则匹配,包含'.' 和 '*'.'.' 匹配任意一个字符,&quo ...

随机推荐

  1. 设计模式——辛格尔顿(Singleton)

    要想正确理解设计模式,首先必须明白它是为了解决什么问题而提出来的. 设计模式学习笔记 --Shulin 转载请注明出处:http://blog.csdn.net/zhshulin 单例模式属于设计模式 ...

  2. HTML contact form with CAPTCHA

    http://www.html-form-guide.com/contact-form/html-contact-form-captcha.html#codedownload

  3. accept功能

    accept()功能 系统调用 accept() 这将是一个有点陌生的地方! 你可以想象发生 这种事情:这是非常远离你通过倾听 (listen()) 的port连接 (connect()) 你的机器. ...

  4. SQL Server提高并发查询效率

    同事写了个程序用创建多个线程使用ado同时对同个数据库进行相同的查询,涉及2张数据表的联查.当线程数非常多的情况下,读取数据的效率就会变得很慢,例如50个线程同时查询大概3000条数据,查询完成后通过 ...

  5. Codeforces Round #256 (Div. 2) A. Rewards

    A. Rewards time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  6. Google Maps Android API v2 (4)- 地图类型

    地图类型 地图内的谷歌地图的Android API的种类有很多.地图的类型管辖地图的整体代表性.例如,地图集通常包含政治地图,专注于显示边界和道路地图,显示了一个城市或地区的所有道路. Android ...

  7. 主攻ASP.NET MVC4.0之重生:ASP.NET MVC使用JSONP

    原文:主攻ASP.NET MVC4.0之重生:ASP.NET MVC使用JSONP 原文地址 http://www.codeguru.com/csharp/.net/net_asp/using-jso ...

  8. statickeyword于C和C++用法

    一.C语言statickeyword两个使用 1).一个功能修改内部使用的变量,函数内的静态变量.这些变量的寿命比功能不再,它是具有一定的函数"状态",使用静态变量的作用通常是不可 ...

  9. Memcahce(MC)系列(两)Linux下一个Memcache安装

    Linux下一个memcache安装 memcache是高性能.分布式的内存对象缓存系统,用于在动态应用中降低数据库负载.提升訪问速度.眼下用memcache解决互联网上的大用户读取是很流行的一种使用 ...

  10. DocFX

    微软开源全新的文档生成工具DocFX 微软放弃Sandcastle有些年头了,微软最近开源了全新的文档生成工具DocFX,目前支持C#和VB,类似JSDoc或Sphinx,可以从源代码中提取注释生成文 ...