题目

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的更多相关文章

  1. 【Permutations】cpp

    题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...

  2. 【Subsets】cpp

    题目: Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset ...

  3. 【Anagrams】 cpp

    题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...

  4. 蓝桥杯 【dp?】.cpp

    题意: 给出一个2*n的方格,当刷完某一个方格的漆后可以且只可以走到相邻的任何一格,即上 下 左 右 左上 左下 右上 右下.可以从任意一个格子开始刷墙,问有多少种刷法,因为随着n的增大方案数会变多, ...

  5. 【Triangle 】cpp

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  6. 【N-Queens】cpp

    题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...

  7. 【Combinations】cpp

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...

  8. 【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 = ...

  9. 【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 ...

随机推荐

  1. MATLAB之折线图、柱状图、饼图以及常用绘图技巧

    MATLAB之折线图.柱状图.饼图以及常用绘图技巧 一.折线图 参考代码: %图1:各模式直接成本预测 %table0-table1为1*9的数组,记录关键数据 table0 = data_modol ...

  2. OSS基本概念介绍

    存储空间(Bucket): 存储空间是用于存储对象(Object)的容器,所有的对象都必须隶属于某个存储空间. 可以设置和修改存储空间属性用来控制地域.访问权限.生命周期等,这些属性设置直接作用于该存 ...

  3. node18 服务器上 pytorch cyclegan 测试有问题,numpy 版本不对

    提示如下错误: module compiled against API version 0xb but this version of numpy is 0xa 尝试的方法: pip install ...

  4. 更新KB915597补丁后导致“您的windows副本不是正版”的解决方案

    更新KB915597补丁后导致“您的windows副本不是正版”的解决方案 解决方法: 运行cw.exe(https://pan.lanzou.com/i05ya8h),直至提示成功: 重新启动操作系 ...

  5. 转载请注明:Windows 系统必备好用软件&工具合集跟推荐 | 老D博客

    Windows 系统必备好用软件&工具合集跟推荐 97 63,371 A+ 所属分类:工具软件 一.浏览器 二.下载软件 三.播放软件 五.电子邮件客户端 六.图片/照片 浏览查看工具 七.文 ...

  6. 关于C#的垃圾回收机制,Finalize和Dispose的区别(自认为很清晰了,有疑问的评论)

    来到个新地方,新学习C#,前面看到C#的垃圾回收,Finalize和Dispose时,总是一知半解,迷迷糊糊.这次好了,前面连续两次面试问到这个问题,脑子里不是很清晰,加上用英文来表达,更是雪上加霜的 ...

  7. python_31_集合

    # 集合是一个无序的,不重复的数据组合,它的主要作用如下: # 去重,把一个列表变成集合,就自动去重了 # 关系测试,测试两组数据之前的交集.差集.并集等关系 s = set([3, 5, 9, 10 ...

  8. IPython安装过程 @win7 64bit

    http://www.360doc.com/content/14/0902/11/16740871_406476389.shtml 为了测验测验一下IPython的应用,今天折腾了好久的从安装包msi ...

  9. C#逻辑运算符

    一.C#逻辑运算符 C#语言的逻辑运算符是对变量的值.表达式的运算结果进行比较,基比较结果为True或False. 二.示例 using System;using System.Collections ...

  10. c++ 软件下载 Dev cpp下载

    下载地址: 链接: https://pan.baidu.com/s/1hsiWQPY 密码: bdpn