这题的思路很巧妙,分两遍扫描,将元素分别和左右元素相比较。

int candy(vector<int> &rattings)
{
int n = rattings.size();
vector<int> incrment(n); int inc = ;
//和左边比较
for (int i = ; i < n; i++)
{
if (rattings[i]>rattings[i - ])
incrment[i] = max(inc++, incrment[i]);
else
inc = ;
}
inc = ;
//和右边比较(把漏掉的第一个补上)
for (int i = n - ; i >= ; i--)
{
if (rattings[i] > rattings[i + ])
incrment[i] = max(inc++, incrment[i]);
else
inc = ;
}
//每人至少一个(将incrment的元素相加,再加上n)
return accumulate(&incrment[], &incrment[] + n, n);
}

leetcode 之Candy(12)的更多相关文章

  1. [LeetCode][Java]Candy@LeetCode

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

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

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

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

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

  4. LeetCode 723. Candy Crush

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

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

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

  6. leetCode练题——12. Integer to Roman

    1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C,  ...

  7. LeetCode 135 Candy(贪心算法)

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

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

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

  9. 【leetcode】Candy

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

随机推荐

  1. BZOJ3437 小P的牧场 【斜率优化dp】

    3437: 小P的牧场 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 1502  Solved: 836 [Submit][Status][Disc ...

  2. JavaScript SandBox沙箱设计模式

    沙箱模式常见于YUI3 core,它是一种采用同一构造器(Constructor)生成彼此独立且互不干扰(self-contained)的实例对象,而从避免污染全局对象的方法. 命名空间 JavaSc ...

  3. dubbox管理中心

    当服务多了,不好管理,可以用管理中心来查看. 管理中心就是一个 web项目(一个war包),可以部署在tomcat下面就能运行了. 用户名密码都是root 每一条服务点开后都可以看生产者和消费者.

  4. 学习tcpIp必备的抓包工具wireshark

    wireshark是一个优秀的抓包工具 ip.src=192.168.10.123  发送http的一端 ip.dst=192.168.10.126 接收http的一端 如下图所示:

  5. springMVC文件上传的三种方法

    这时:commonsmultipartresolver 的源码,可以研究一下 http://www.verysource.com/code/2337329_1/commonsmultipartreso ...

  6. ClusterId read in ZooKeeper is null 处理

    ClusterId read in ZooKeeper is null. Re-running the program after fixing issue 1 will result in the ...

  7. Java中String的hash函数分析

    转载自:http://blog.csdn.net/hengyunabc/article/details/7198533 JDK6的源码: [java] view plaincopy /** * Ret ...

  8. Git新手上路,让你快速掌握Git的基本使用

    github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.这对于一般人来说公共仓库就已经足够了. 1.注册账户以及创建仓库     要想 ...

  9. mysql的数据库 索引

    1.两种主要的引擎:MyISAM和InnoDB 2.如何查看自己的表是什么类型:http://www.cnblogs.com/luosongchao/archive/2013/05/23/309592 ...

  10. java File (文档流)

    一.   数据流的基本概念 1.数据流 在Java中把不同的数据源与程序之间的数据传输都抽象表述为“流”(stream),以实现相对统一和简单的输入/输出操作方式.传输中的数据就像流水一样,也称为数据 ...