【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 ...
随机推荐
- oracle中查找与已知表的数据库对象
在此次情况中,业务顾问就给我提供了一张客户公司客户化的Form,然后让找出界面上的数据是怎样生成的. 首先我们从EBS form 界面上找到了界面的数据来源于一张表ks_so_line_margin_ ...
- 网络编程——基于UDP的网络化CPU性能检测
网络化计算机性能检测软件的开发,可对指定目标主机的CPU利用率进行远程检测,并自动对远程主机执行性能指标进行周期性检测,最终实现图形化显示检测结果. 网络通信模块:(客户端类似,因为udp是对等通信) ...
- pta 编程题14 Huffman Codes
其它pta数据结构编程题请参见:pta 题目 题目给出一组字母和每个字母的频数,因为哈夫曼编码不唯一,然后给出几组编码,因为哈夫曼编码不唯一,所以让你判断这些编码是否符合是哈夫曼编码的一种. 解题思路 ...
- 实现带查询功能的ComboBox控件
实现效果: 知识运用: ComboBox控件的AutoCompleteMode属性 public AutoCompleteMode AutoCompleteMode{get;set;} //属性值为枚 ...
- AngularJS 历经实例
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- phpstorm —— Xdebug 的配置和使用
给phpstorm 配置Xdebug(Xdebug 是 PHP 的一个扩展, 用于帮助调试和开发.它包含一个与 ide 一起使用的单步调试器.它升级了 PHP 的 var_dump () 功能) 这篇 ...
- POI Excel 插入新的行,下面的行动态移动
在做Excel 模板时,会有遇到 模板行数不固定,如下图 需要在行次4下面再插入一行:注意:(插入的行如果是下面空白行,需要创建行) 解决方法是使用shifRows方法,第1个参数是指要开始插入的 ...
- 【原创】大数据量时生成DataFrame避免使用效率低的append方法
转载请注明出处:https://www.cnblogs.com/oceanicstar/p/10900332.html ★append方法可以很方便地拼接两个DataFrame df1. ...
- 微信小游戏 demo 飞机大战 代码分析 (一)(game.js, main.js)
微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...
- C程序设计语言 -- 运算符优先级
1. 运算符分类 算术运算符 [+, -,*, /, % , ++, --] 关系运算符 [>, >=, <, <=] 相等性运 ...