Leetcode 动态规划 Candy
本文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的更多相关文章
- 快速上手leetcode动态规划题
快速上手leetcode动态规划题 我现在是初学的状态,在此来记录我的刷题过程,便于以后复习巩固. 我leetcode从动态规划开始刷,语言用的java. 一.了解动态规划 我上网查了一下动态规划,了 ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
- [LeetCode] 动态规划入门题目
最近接触了动态规划这个厉害的方法,还在慢慢地试着去了解这种思想,因此就在LeetCode上面找了几道比较简单的题目练了练手. 首先,动态规划是什么呢?很多人认为把它称作一种"算法" ...
- LeetCode 动态规划
动态规划:适用于子问题不是独立的情况,也就是各子问题包含子子问题,若用分治算法,则会做很多不必要的工作,重复的求解子问题,动态规划对每个子子问题,只求解一次将其结果保存在一张表中,从而避免重复计算. ...
- [LeetCode] 723. Candy Crush 糖果消消乐
This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...
- LeetCode 723. Candy Crush
原题链接在这里:https://leetcode.com/problems/candy-crush/ 题目: This question is about implementing a basic e ...
- [LeetCode] 723. Candy Crush 糖果粉碎
This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...
- LeetCode动态规划题总结【持续更新】
以下题号均为LeetCode题号,便于查看原题. 10. Regular Expression Matching 题意:实现字符串的正则匹配,包含'.' 和 '*'.'.' 匹配任意一个字符,&quo ...
随机推荐
- oracle错误之 ora-01017
ora-01017 现象描述: scott用户和其它建立的用户,都登的上.但sys和system用户登录不上 方案一(试过,不行): 1,打开目录:F:\app\Administrator\produ ...
- C# Windows Phone 8 WP8 开发,取得手机萤幕大小两种方法。
原文:C# Windows Phone 8 WP8 开发,取得手机萤幕大小两种方法. 一般我们在开发Windows Phone App时,需要取得萤幕的大小来自定义最佳化控制项的大小,但是开如何取得萤 ...
- openstack dhcp调试
openstack的dhcpserver默认值dnsmasq软件实施,经ps -ef | grep dnsmasq 查看.当虚拟机启动过程启动dhcp求,日志可以是在主机系统日志: May 23 22 ...
- 如何设置多个同一页的tinymce编辑
的页面设置多个tinymce编辑器 This example shows how to setup multiple editors on the same page and with differe ...
- 3g自己主动更新网卡驱动web完架构文档
几年前写. 看它是否是用得上 1 简单介绍 本文档具体描写叙述了基于ASP.NET平台和IIS服务的T-Mobile自己主动更新系统的实现框架. 本文档主要从技术架构和业务架构两个方面来着手来描写叙 ...
- android传感器;摇抽奖功能
package com.kane.sensortest; import java.util.Random; import android.hardware.Sensor; import android ...
- sails 相关文章
Node 框架之sails http://cnodejs.org/topic/555c3c82e684c4c8088a0ca1
- UVa 572 Oil Deposits(DFS)
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil ...
- 集合hashCode()方法和equals()办法
1.哈希码: Object中的HashCode方法会返回该对象的的内存真实地址的整数化表示,这个形象的不是真正抵制的整数值就是哈希码. 2.利用哈希码向集合中插入数据的顺序? ...
- C标签之forEach
<c:forEach>标签用于通用数据循环,它有下面属性 属 性 描 述 是否必须 缺省值 items 进行循环的项目 否 无 begin 開始条件 否 0 end 结束条件 否 集合中的 ...