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

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. Android中WebView的跨域漏洞分析和应用被克隆问题情景还原(免Root获取应用沙盒数据)

    一.前言 去年年底支付宝的被克隆漏洞被爆出,无独有偶就是腾讯干的,其实真正了解这个事件之后会发现,感觉是针对支付宝.因为这个漏洞找出肯定花费了很大劲,主要是因为支付宝的特殊业务需要开启了WebView ...

  2. BZOJ3238:[AHOI2013]差异——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3238 https://www.luogu.org/problemnew/show/P4248 参考 ...

  3. BZOJ2002:[HNOI2010]弹飞绵羊——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2002 https://www.luogu.org/problemnew/show/P3203 某天, ...

  4. ural1297 求最长回文子串 | 后缀数组

    #include<cstdio> #include<algorithm> #include<cstring> #define N 20005 using names ...

  5. AtCoder Grand Contest 031 B - Reversi(DP)

    B - Reversi 题目链接:https://atcoder.jp/contests/agc031/tasks/agc031_b 题意: 给出n个数,然后现在你可以对一段区间修改成相同的值,前提是 ...

  6. UVA315:Network(求割点)

    Network 题目链接:https://vjudge.net/problem/UVA-315 Description: A Telephone Line Company (TLC) is estab ...

  7. Spring 容器AOP的实现原理——动态代理

    参考:http://wiki.jikexueyuan.com/project/ssh-noob-learning/dynamic-proxy.html(from极客学院) 一.介绍 Spring的动态 ...

  8. uboot 的命令体系

    1.代码位置 (1)uboot命令体系的实现代码在uboot/common/cmd_xxx.c中.有若干个.c文件和命令体系有关.(还有command.c  main.c也是和命令有关的) 2.传参方 ...

  9. UVA 1213 Sum of Different Primes

    https://vjudge.net/problem/UVA-1213 dp[i][j][k] 前i个质数里选j个和为k的方案数 枚举第i个选不选转移 #include<cstdio> # ...

  10. [USACO13NOV] Pogo-Cow

    https://www.luogu.org/problem/show?pid=3089 题目描述 In an ill-conceived attempt to enhance the mobility ...