【Candy】cpp
题目:
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?
代码:
class Solution {
public:
int candy(vector<int>& ratings) {
const size_t len = ratings.size();
std::vector<int> candyNum(len);
// from left to right
for (size_t i = ; i < len; ++i){
if (ratings[i] > ratings[i-]) candyNum[i] = candyNum[i-] + ;
}
// from right to left
for (size_t i = ; i < len; ++i){
if (ratings[len-i-]>ratings[len-i]){
candyNum[len-i-] = std::max(candyNum[len-i]+,candyNum[len-i-]);
}
}
return std::accumulate(candyNum.begin(), candyNum.end(), len);
}
};
Tips:
大体思路是greedy。题目的需求可以分解为如下两个条件:
a. ratings从左向右的子递增序列,candy也要是递增的
b. ratings从右向左的子递增序列,candy同样也是递增的
具体:
1. 从左往右遍历一次,保证从左→右的递增序列,candy都是递增1的(相当于先对条件a进行了一次greedy,保证了条件a是充分的)
2. 从右往左遍历一次,保证从右→左的递增序列,candy也是至少递增1的(相当于再对条件b进行了一次greedy,保证了条件b是充分的,但是不能破坏条件a成立)
代码中一条关键语句如下:
std::max(candyNum[len-i]+1,candyNum[len-i-1]);
举个例子,ratings = {4,2,3,4,1}
从左往右走完第一遍 则candyNum = {0,0,1,2,0}
现在开始从右往左走,如果没有max这条语句,则走完右数第二个元素就变成了 candyNum = {0,0,1,1,0}
这样为了满足条件b就破坏了条件a,所以max这条语句是必要的。
从总体思路上来说:两个greedy并不能直接得到全局的最优解,需要调节两个greedy之间的矛盾,而max这条语句就是调节矛盾的。
================================================
第二次过这道题,大体思路记得比较清楚,就是红字的部分,两个greedy之间不能破坏彼此的条件。
class Solution {
public:
int candy(vector<int>& ratings) {
vector<int> candys(ratings.size(),);
// from left to right
for ( int i=; i<ratings.size(); ++i )
{
if ( ratings[i]>ratings[i-] ) candys[i] = candys[i-]+;
}
// from right to left
for ( int i=ratings.size()-; i>; --i )
{
if ( ratings[i-]>ratings[i] )
{
candys[i-] = std::max( candys[i-], candys[i]+ );
}
}
return accumulate(candys.begin(), candys.end(), );
}
};
【Candy】cpp的更多相关文章
- 【Permutations】cpp
题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...
- 【Subsets】cpp
题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset ...
- 【Anagrams】 cpp
题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...
- 蓝桥杯 【dp?】.cpp
题意: 给出一个2*n的方格,当刷完某一个方格的漆后可以且只可以走到相邻的任何一格,即上 下 左 右 左上 左下 右上 右下.可以从任意一个格子开始刷墙,问有多少种刷法,因为随着n的增大方案数会变多, ...
- 【Triangle 】cpp
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- 【N-Queens】cpp
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...
- 【Combinations】cpp
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- 【4Sum】cpp
题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- 【3Sum】cpp
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
随机推荐
- C# 发Domino邮件 报错误 Password or other security violation for database 的解决方案
错误提示: Password or other security violation for database ******* 问题产生的描述: 之前C#发邮件是好的 加上了附件部分代码之后,出现了这 ...
- 【Java/Android性能优5】 Android ImageCache图片缓存,使用简单,支持预取,支持多种缓存算法,支持不同网络类型,扩展性强
本文转自:http://www.trinea.cn/android/android-imagecache/ 主要介绍一个支持图片自动预取.支持多种缓存算法.支持二级缓存.支持数据保存和恢复的图片缓存的 ...
- 【Android开发笔记】返回上层Activity的正确打开方式
技术支持 http://stackoverflow.com/questions/12276027/how-can-i-return-to-a-parent-activity-correctly 首先, ...
- Redis基础对象
Redis 中每个对象都由一个 redisObject 结构表示 typedef struct redisObject { //类型 unsigned type:; //编码 unsigned enc ...
- IOS UISwitch控件的基本使用
* UISwitch继承自UIControl,因此也能像UIButton一样监听一些事件,比如状态改变事件* UISwitch可以通过拖线监听状态改变* UISwitch可以通过addTarget:. ...
- openlayers 初步认识(转)
OpenLayers是一个开源的js框架,用于在您的浏览器中实现地图浏览的效果和基本的zoom,pan等功能.OpenLayers支持的地图来源 包括了WMS,GoogleMap,KaMap,MSVi ...
- DOS&8086微处理器
DOS DOS环境,需要安装dosemu来模拟DOS环境(Ubuntu的应用商店就有),为了编写汇编,还需要DEBUG.MASM.LINK等汇编语言开发工具.如果你嫌麻烦,推荐使用实验楼已搭好的免费的 ...
- 问题 F: 等比数列
问题 F: 等比数列 时间限制: 1 Sec 内存限制: 64 MB提交: 2699 解决: 1214[提交][状态][讨论版][命题人:外部导入] 题目描述 已知q与n,求等比数列之和: 1+q ...
- Java设计模式学习——设计原则
第一章 设计原则 1.开闭原则 一个软件实体,像类,模块,函数应该对扩展开放,对修改关闭 在设计的时候,要时刻考虑,让这个类尽量的好,写好了就不要去修改.如果有新的需求来,在增加一个类就完事了,原来的 ...
- ubuntu web server ipython notebook install
http://blog.csdn.net/yehuohan/article/details/51389966 ipython notebook installhttp://blog.csdn.net/ ...