分糖果

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?

题目意思:

有N个小孩站成一条直线,每一个小孩都有一个等级的属性。按下面要求给小孩分糖果:

  • 每一个小孩都必须有至少一个糖。
  • 相邻的等级高的小孩得到更多的糖果。(相对他的前后的人)

求你最少要给出去多少个糖果。

解题思路:

N个小孩加上属性的话,有点类似于骆驼背的那种形状。我们新建一个用来表示每个小孩得多少糖果的大小为N的vector,和题目中给的等级vector对应。

然后两次遍历原有的vector,第一遍从前面向后面遍历,第二遍从后往前。

第一遍的目的是保证队伍后面的人如果比前面的人等级高的话,后面的人的糖果比前面的人多一个,否则后面的人糖果设为1个。

但是假如有类似于  3 4 5 4 3 这种等级分布的话,第一遍遍历后result变成了 1 2 3 1 1 ,而事实上应该是 1 2 3 2 1 ,第一遍遍历时没有考虑到后两个应该是递减的关系,所以第二遍遍历的目的就是处理这种本来应该是递减顺序全都成了1的情况。

代码如下:

 class Solution {
public:
int candy(vector<int> &ratings) {
int len = ratings.size();
vector<int> result(len,); int ret = ;
if(len == ){
return ret;
}
//第一遍
for(int i = ; i < len; i++){
if(ratings[i] > ratings[i-]){
result[i] = result[i-] +;
}
}
//第二遍
ret = result[len - ];
for(int i = len - ; i >= ; i--){
if(ratings[i] > ratings[i+]){
result[i] = max(result[i],result[i+]+);
}
ret += result[i];
} vector<int>::iterator it = result.begin();
for(; it != result.end();it++){
cout << *it << "--";
}
return ret;
}
};

【LeetCode练习题】Candy的更多相关文章

  1. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

  2. Leetcode练习题Remove Element

    Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...

  3. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  4. (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重

    原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...

  5. [LeetCode] 723. Candy Crush 糖果消消乐

    This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...

  6. LeetCode 723. Candy Crush

    原题链接在这里:https://leetcode.com/problems/candy-crush/ 题目: This question is about implementing a basic e ...

  7. [LeetCode] 723. Candy Crush 糖果粉碎

    This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...

  8. LeetCode 135 Candy(贪心算法)

    135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...

  9. 【leetcode】Candy(hard) 自己做出来了 但别人的更好

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

随机推荐

  1. 如何导出sqlserver中的表数据,sqlserver2008

    sqlserver数据库中的表数据,我们通常想使用一下,可是怎样获取这些数据呢? 1.选择任务->生成脚本 2.选择数据库 3.设置配置,让编写数据的脚本为true 4.保存为sql 5.完成 ...

  2. DDMS files not found:hprof-conv.exe的解决办法

    或者是you must restart adb and eclipse这类错误 原因:一般是豌豆荚之类的软件影响的,所以,以后要慎用了. 解决方案:先找一下在sdk\tools目录下是否有hprof- ...

  3. IA32与x86

    64位机器相比于32位优点 ①访问虚拟地址范围更大 ②更多的寄存器 ③过程较简单 ④采用条件传送指令 详细看:http://baike.baidu.com/link?url=DoRp7iW_z3cE6 ...

  4. Remove Duplicates from Sorted Array 解答

    Question Given a sorted array, remove the duplicates in place such that each element appear only onc ...

  5. Best Time to Buy and Sell Stock 解答

    Question Say you have an array for which the ith element is the price of a given stock on day i. If ...

  6. fourinone分布式缓存研究和Redis分布式缓存研究

    最近在写一个天气数据推送的项目,准备用缓存来存储数据.下面分别介绍一下fourinone分布式缓存和Redis分布式缓存,然后对二者进行对比,以供大家参考. 1  fourinone分布式缓存特性 1 ...

  7. 【转】android 电容屏(一):电容屏基本原理篇

    关键词:android  电容屏 tp  ITO 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV310(samsung exynos 42 ...

  8. UGUI Toggle控件

    今天我们来看看Toogle控件, 它由Toogle + 背景 + 打勾图片 + 标签组成的. 它主要用于单选和多选 属性讲解: Is On: 代表是否选中. Toogle Transition: 在状 ...

  9. Unity 音乐播放全局类

    今天晚了LOL, 发现里面的声音系统做得很不错,于是最近就写了一份反正以后也用的到,2D音乐全局播放. 项目跟PoolManager对象池插件结合了. 解决的问题: 1. 已经播放的声音,可以马上暂停 ...

  10. 从3dmax中导入模型到UDK Editor(供个人备忘)

    笔记从3dmax中导入模型到UDK Editor 1)      在3dmax中导出 2)      选择FBX格式,保存 3)      在UDK中打开content browser,自己选个pac ...