分糖果

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. java实现文件编码监测

    java实现文件编码监测 最近在做一个文档的翻译项目,可文档的编码不知道,听头疼的.尝试了很多方法最后发现JCharDet这个工具可以轻松解决这个问题.于是作此笔记希望日后提醒自己以及帮助又需要的人. ...

  2. iframe父子页面互调方法和属性

    1.iframe子页面调用 父页面js函数 子页面调用父页面函数只需要写上window.praent就可以了.比如调用a()函数,就写成: window.parent.a(); 子页面取父页面中的标签 ...

  3. POJ 3259 Wormholes( bellmanFord判负环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36425   Accepted: 13320 Descr ...

  4. hdu 5465 Clarke and puzzle(前缀和,异或,nim博弈)

    Problem Description Clarke is a patient with multiple personality disorder. One day, Clarke split in ...

  5. Capture the Flag(模拟)

    Capture the Flag Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In computer se ...

  6. [Node.js]在windows下不得不防的小错误

    TypeError: Arguments to path.join must be strings at f (path.js:204:15) at Object.filter (native) at ...

  7. URL 对特殊字符的处理

    看到很多人说可以通过转移字符来进行转义,避免URL在请求的时候出错. 现在有了更好的方法了.不然还不得把半个ASCII码表给进行转义了! import java.io.UnsupportedEncod ...

  8. ASP.NET在实际开发中验证码的用法

    在网上有看到很多关于验证码的代码,很多都只是生成一张验证码图片,然而在实际登陆验证模块,验证码要怎么添加进去或者说怎么运用.和实际项目开发中要怎么使用验证码,我自己总结了几点. 一.在实际开发登陆模块 ...

  9. MVC不错的学习资料

    MVC不错的学习资料: http://www.cnblogs.com/darrenji/

  10. (ZZ)WPF经典编程模式-MVVM示例讲解

    http://www.cnblogs.com/xjxz/archive/2012/11/14/WPF.html 本篇从两个方面来讨论MVVM模式: MVVM理论知识 MVVM示例讲解 一,MVVM理论 ...