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?

思路:neighbor=>元素值与前、后元素有关=>

第一次前向扫描:保证high rate元素比左邻居糖多;

第二次后向扫描:保证high rate元素比右邻居糖多;

class Solution {
public:
int candy(vector<int> &ratings) {
vector<int> new_ratings(ratings.size(), );
for(int i = ; i < ratings.size(); i++) //for the first time, scan from beginning
{
if(ratings[i] > ratings[i-])
{
new_ratings[i] = new_ratings[i-]+;
} }
for(int i=ratings.size()-;i>=;i--){ //for the second time, scan from the end
if(ratings[i]>ratings[i+]&&new_ratings[i]<=new_ratings[i+]){
new_ratings[i]=new_ratings[i+]+;
}
}
int sum=;
for(int i=;i<new_ratings.size();i++){
sum+=new_ratings[i];
} return sum;
}
};

135. Candy(Array; Greedy)的更多相关文章

  1. LeetCode 135 Candy(贪心算法)

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

  2. leetcode 135. Candy ----- java

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

  3. Leetcode#135 Candy

    原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...

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

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

  5. 135. Candy

    题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...

  6. [leet code 135]candy

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

  7. 55. Jump Game (Array; Greedy)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. 【LeetCode】135. Candy

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

  9. Java for LeetCode 135 Candy

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

随机推荐

  1. 40+个对初学者非常有用的PHP技巧

    1.不要使用相对路径,要定义一个根路径 这样的代码行很常见: require_once('../../lib/some_class.php'); 这种方法有很多缺点: 它首先搜索php包括路径中的指定 ...

  2. kbuild-(directory)

    -INDEX - this file: info on the kernel build process kbuild.txt - developer information on kbuild kc ...

  3. (转)winform安装项目、安装包的制作、部署

    本文转载自:http://zhan.renren.com/cxymst?gid=3602888498037535727&from=post&checked=true 1,解决方案—添加 ...

  4. Java中的<< 和 >> 和 >>> 详细分析

    <<表示左移移,不分正负数,低位补0: 注:以下数据类型默认为byte-8位 左移时不管正负,低位补0 正数:r = 20 << 2 20的二进制补码:0001 0100 向左 ...

  5. 数据报表类(BI)项目测试应该如何去啃?

    测试工作是一项十分枯燥的工作,与之相对的测试人员必须有足够的耐心.绝对的细心等素质才能完美的完成这项工作. 从最初的瀑布模式,到如今风靡的敏捷,Devops等:从最初的最后一道关卡到渗透至各个流程,再 ...

  6. 20181104_C#线程之Thread_ThreadPool_使用Thread实现回到和带参数的回调

    C#   .net  Framework多线程演变路径: 1.0    1.1 时代使用Thread 2.0    时代使用ThreadPool 3.0    时代使用Task 4.0    时代使用 ...

  7. VMware 虚拟机中添加新硬盘的方法(转载)

    随着在虚拟机中存储的东西的逐渐的增加,虚拟机的硬盘也逐渐告急,因此急需拓展一块新的虚拟磁盘.以下便是在VMware 中添加新的虚拟磁盘的方法:   一.VMware新增磁盘的设置步骤 (建议:在设置虚 ...

  8. PolyBase 指南

    PolyBase 是一种可通过 t-sql 语言访问数据库外部数据的技术.PolyBase is a technology that accesses data outside of the data ...

  9. Confluence 5.4.2安装

    默认安装路径 /opt/atlassian/confluence/bin Confluence是Atlassian公司出品的团队协同与知识管理工具. Confluence是一个专业的企业知识管理与协同 ...

  10. 脱壳系列(五) - MEW 壳

    先用 PEiD 看一下 MEW 11 1.2 的壳 用 OD 载入程序 按 F8 进行跳转 往下拉 找到这个 retn 指令,并下断点 然后 F9 运行 停在该断点处后再按 F8 右键 -> 分 ...