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. npm 可执行模块的开发&&私服发布

    备注:    大家日常在使用npm 安装依赖的时候有一些是命令行工具,比如vue-cli,具体的开发比较简单,同时 可以基于此开发一些脚手架,方便开发. 1. 项目初始化 npm init 备注:按照 ...

  2. centos6.6安装SVN服务器(2015/3/7)

    一.安装#yum install subversion  判断是否安装成功  [root@]# svnserve --version有了SVN软件后还需要建立SVN库.#mkdir /opt/svn/ ...

  3. 谈谈JS中的高级函数

    博客原文地址:Claiyre的个人博客如需转载,请在文章开头注明原文地址 在JavaScript中,函数的功能十分强大.它们是第一类对象,也可以作为另一个对象的方法,还可以作为参数传入另一个函数,不仅 ...

  4. Tomcat(64位)免安装版的环境安装与配置

    本篇博客主要介绍Tomcat(64位)免安装版的环境安装与配置,该篇文章同样适合于32位Tomcat免安装版的环境安装与配置. 该篇博客中的大部分内容同百度经验中的<出现unable to op ...

  5. Web Service简介(一)

    这篇博文,我们对Web Service进行一个简单的介绍和认识,作为Web Service的入门.在学习之前,你需要对HTML和XML有基本的了解,Web Service并不难,而且非常的简单. 什么 ...

  6. Liveqrcode活码系统设计

    活码是一种二维码,可以通过后台配置让用户扫码时跳转到不同的网址.除了二维码生成接口,本站还实现了多租户的活码配置接口,以及活码后台jar包,详见二维码接口. 二维码生成使用了zxing三方包实现,活码 ...

  7. kubernetes 学习 常用命令

    1  kubectl get nodes      #查看nodes节点情况 2  kubectl describe  node node_name_XXXX           # 查看nodes详 ...

  8. C++ 单链表操作总结

    第一.单链表的定义和操作 #include <iostream> using namespace std; template <typename T> struct Node ...

  9. 使用product_user_profile来实现用户权限的设定

    我们有时候在以普通用户登录SQL*Plus的时候,会碰到下面的错误提示: Error accessing PRODUCT_USER_PROFILE Warning: Product user prof ...

  10. 你知道的,javascript语言的执行环境是"单线程模式",这种模式的好处是实现起来比较简单,执行环境相对单纯;坏处是只要有一个任务耗时很长,后面的任务都必须排队等着,会拖延整个程序的执行,因此很多时候需要进行“异步模式”,请列举js异步编程的方法。

    回调函数,这是异步编程最基本的方法. 事件监听,另一种思路是采用事件驱动模式.任务的执行不取决于代码的顺序,而取决于某个事件是否发生. 发布/订阅,上一节的"事件",完全可以理解成 ...