【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 ...
随机推荐
- Android——dpi相关知识总结
1.术语和概念 术语 说明 备注 Screen size(屏幕尺寸) 指的是手机实际的物理尺寸,比如常用的2.8英寸,3.2英寸,3.5英寸,3.7英寸...... nexus4手机是4.7英寸 As ...
- Java static修饰符小记
首先我们明确一点:java是一个纯面向对象的编程语言,它的每一个文件都视为一个类,当我们创建一个对象的时候,就是在调用一个文件,那么这个时候,这个文件(类)里的一些东西,它是需要通过对象来使用或访问, ...
- 杭电acm刷题顺序
最近兴趣来了,闲暇之余,回顾大学期间刷过的杭电acm那些入门级别的题,以此巩固基础知识! 以下参考刷题顺序,避免入坑 原文传送门:https://blog.csdn.net/liuqiyao_01/a ...
- 美国移民局的I797表原件和I129表是什么呢
I-129表,Petition for a Non-immigrant Worker,即非移民工作许可申请表I797 表 ,Original L1-1A approval notice L1签证批准通 ...
- OpenSSL context 的几个参数
NAME SYNOPSIS DESCRIPTION NOTES BUGS RETURN VALUES EXAMPLES SEE ALSO NAME SSL_CTX_set_verify, SSL_se ...
- ASP.NET中登陆验证码的生成和输入验证码的验证
一:验证码的生成实现代码 protected void Page_Load(object sender, EventArgs e) { string validateCode = ...
- linux 命令——35 ln(转)
ln 是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要 ...
- iOS 不同的崩溃类型
http://m.blog.csdn.net/kangguang/article/details/62501490 用汇编语言编写的软件跟用脚本或标记语言编写的Web应用的差别在于,前者在出现问题时会 ...
- SpringMVC-响应数据和结果视图
返回值分类 1. 字符串 controller 方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址. 2. void 在 controller 方法形参上可以定义 request 和 ...
- 我的Linux学习之路的感悟
首先要跟大家说声抱歉,这么久一直没有更新,有负大家对我的期望. 半年的Linux运维的学习到目前已工作一个月零9天,这一路走来的艰辛和挣扎只有自己最清楚. 首先要感谢公司的同事的宽容接纳和耐心指点.感 ...