【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 ...
随机推荐
- 父类和子类以及super关键字
super和this关键字的特点类似:super代表的是父类对象的引用. 当子父类的成员出现同名时,可以通过super来进行区分. 子类的构造方法中,通过super关键字调用父类的构造方法. publ ...
- DELL R730安装ESXI虚拟化
dell安装esxi需要dell官方提供的镜像文件地址:http://www.dell.com/support/article/us/en/04/SLN290857/dell%E5%AE%9A%E5% ...
- (转) HTTP Request header
HTTP Request header 当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Perl, AJAX 等等. 无论Web技术在未来如何发展,理解Web程序之间通信的基 ...
- weka属性选择使用
醉了--- package edu.dcy.weka; import java.io.FileWriter; import java.util.ArrayList; import java.util. ...
- [Asp.Net] MVC 和Web API Action 获取参数的区别
Asp.net MVC 和web api 的action 在获取从前台传入的数据是有很大不同 前台使用ajax的方式向后台发起post的请求 Content-Type:application/json ...
- pta 编程题16 Saving James Bond - Easy Version
其它pta数据结构编程题请参见:pta 题目 主要用到了深度优先搜索. #include <iostream> using namespace std; struct Vertex { i ...
- vue中使用setTimeout
在vue的函数中使用setTimeout self.distroyTimeout = setTimeout(()=>{ self.initData() },1000) 这时清除setTimeou ...
- Codeforces Round #327 (Div. 2) C Median Smoothing(找规律)
分析: 三个01组合只有八种情况: 000 s001 s010 0011 s100 s101 1110 s111 s 可以看出只有010,101是不稳定的.其他都是稳定的,且连续地出现了1或0,标记为 ...
- 【洛谷2468】[SDOI2010] 粟粟的书架(二合一)
点此看题面 大致题意: 问你选取一个矩形区间内至少几个数,才能使它们的和\(\ge H_i\). 二合一 根据数据范围,比较显然能看出它是一道二合一的题目. 对于第一种情况,\(R,C\le 200\ ...
- 【转】转自微信公众号 JavaScript 复杂判断的更优雅写法
与微信公众号看到一篇js复杂判断的文章,对我启发很大,故转到博客园以供后期不断学习并应用于项目.原文地址:https://mp.weixin.qq.com/s/ClFDRj4MnAxv1dJ5VWKS ...