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

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. BZOJ3140:[HNOI2013]消毒——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3140 https://www.luogu.org/problemnew/show/P3231 最近在 ...

  2. 【HASH】【UVA 10125】 Sumset

    传送门 Description 给定一个整数集合S,求一个最大的d,满足a+b+c=d,其中a,b,c,d∈S Input 多组数据,每组数据包括: 第一行一个整数n,代表元素个数 下面n行每行一个整 ...

  3. JavaScript是如何实现继承的

    JavaScript中的function是万能的,除了用于的函数定义,也可以用于类的定义.JavaScript的继承,说起来也是有点怪,没有public,private等访问控制修饰,也没有imple ...

  4. Consul 入门

    1. 什么是Consul? Consul 有很多组件,对于整体来说,它是一个服务发现和服务配置的工具,它提供了一下特性: 服务发现 健康检查 KV存储 多数据中心 2.安装Consul 以下是在 Ce ...

  5. crontab使用进程锁解决冲突

    想到一个问题,如果在crontab里有个定时任务设置为一分钟执行一次,但是它执行的时间可能会超过一分钟,此时crontab一分钟后会再次运行该脚本吗?这样会不会出现冲突呢?网上找了下,说可以用Linu ...

  6. poi-对于word的操作(二)

    poi对于word文本的底纹和下划线的样式的展现 package poi.test; import java.io.FileOutputStream; import java.math.BigInte ...

  7. 51Nod 1002 数塔取数问题

    Input示例 4 5 8 4 3 6 9 7 2 9 5 Output示例 28 DP: 递推式: dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+arr[i][j]; ...

  8. Tomcat报错java.lang.ClassNotFoundException: 2localhost.org.apache.juli.FileHandler

    Can't load log handler "1catalina.org.apache.juli.FileHandler" java.lang.ClassNotFoundExce ...

  9. Long Parameter List(过长参数列)---要重构的味道

      一个函数,它的参数过多是不好的,不好维护和修改,易读性也差,容易出错.       消除过长参数的方法,有如下:        1.在面向对象中,你可以传递一个对象给函数,函数通过访问对象来获得参 ...

  10. PAT L1-009 N个数求和(运用GCD进行通分)

    题目链接:https://www.patest.cn/contests/gplt/L1-009 题目: 本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数“分子/分母”的形式给出的,你 ...