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. div,css常用技术

    1,<div></div>一张图作为背景的用法: 必须指定width,height,background属性 .smallCircle{ margin-top: 25px;   ...

  2. Jacoco在eclipse上的集成使用

    随着敏捷开发的流行,编写单元测试已经成为业界共识.但如何来衡量单元测试的质量呢?有些管理者片面追求单元测试的数量,导致底下的开发人员投机取巧,编写出大量的重复测试,数量上去了,质量却依然原地踏步.相比 ...

  3. bower.json 的版本范围

    bower.json 的版本范围 有小伙伴问 ~2.2.0 什么意思. 而且在git 的tags 中没有了 2.2.0 版本,怎么样? 实际上 ~2.2.0 的意思是 >=2.2.0 <2 ...

  4. Zookeeper--Watcher 和 ACL

    Zookeeper--Watcher 和 ACL Watcher (观察) Zookeeper中的znode可以被监控,这是zk的核心特性. 通过exists,getChildren和getData这 ...

  5. sorl基本原理

    索引原理 场景:小时候我们都使用过新华字典,妈妈叫你翻开第38页,找到“坑爹”所在的位置,此时你会怎么查呢?毫无疑问,你的眼睛会从38页的第一个字开始从头至尾地扫描,直到找到“坑爹”二字为止.这种搜索 ...

  6. PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  7. 初学者手册-Sublime Text常用快捷键

    Alt + F3 :找出当前文档中所有被划选的词语,若文档很大的话,可能会导致Sublime Text崩溃. Ctrl + kkk :删除当前行光标至行尾的所有内容. End: 光标跳至行尾. Hom ...

  8. Windows下安装GCC

    1.GCC编译器的选择 Windows下最常见的安装GCC的方式有两种:Cygwin和MinGW.本文主要介绍MinGW的安装配置. 2.下载MinGW 下载地址:http://sourceforge ...

  9. cloudera manager卸载流程

    注意:卸载Cloudera Manager后,根据需要保留或者删除集群中的Hadoop数据.下面的命令没有删除Hadoop数据,可以在控制台的Hadoop 和MapReduce /配置/选项卡,查看H ...

  10. Tkinter Cursors

      Tkinter Cursors:   Python的Tkinter的支持很多不同的鼠标光标的数字.确切的图形可能会有所不同,根据您的操作系统. 这里是大量有趣的的名单: "arrow&q ...