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?

注意,像 6 5 5 4 这样的 可以分 2 1 2 1 就是数字相同的不需要糖果数相同

我的思路,分别用两个vector存储递增和递减所需要的糖果,最终结果取两个的最大值

如 权重    6 6 1 5 4 4 3 7 4 2 2 2 5 4 3 5

找递增     1 1 1 2 1 1 1 2 1 1 1 1 2 1 1 2   从前向后找,初始都是1,遇到递增的就加上他前面查找的数字

找递减     1 2 1 2 1 2 1 3 2 1 1 1 3 2 1 1   从后向前找,初始都是1,遇到递增的就加上他前面查找的数字

最终        1 2 1 2 1 2 1 3 2 1 1 1 3 2 1 2

int candy(vector<int> &ratings) {
int ans = ;
vector<int> candysUp(ratings.size(), );
vector<int> candysDown(ratings.size(), );
for(int i = ; i < ratings.size(); i++) //查找递增
{
if(ratings[i] > ratings[i - ])
{
candysUp[i] += candysUp[i - ];
}
}
for(int i = ratings.size() - ; i >= ; i--) //查找递减
{
if(ratings[i + ] < ratings[i])
{
candysDown[i] += candysDown[i + ];
}
} for(int i = ; i < ratings.size(); i++)
{
ans += max(candysUp[i], candysDown[i]);
}
return ans;
}

大神只循环一遍的思路:

其实整体思想跟上面一样,但是上面需要循环很多次是因为递增和递减分别处理。因为递减时不知道最大的数应该加几。

大神的思路是递增的就像上面那样加,但是递减的,先都加1,如果后面仍是递减的,再把之前少加的给加回来(如前面有nSeqLen个数字少加了1,就把答案直接多加一个nSeqLen)。

    //大神循环一遍的代码
int candy2(vector<int> &ratings) {
int nCandyCnt = ;///Total candies
int nSeqLen = ; /// Continuous ratings descending sequence length
int nPreCanCnt = ; /// Previous child's candy count
int nMaxCntInSeq = nPreCanCnt;
if(ratings.begin() != ratings.end())
{
nCandyCnt++;//Counting the first child's candy.
for(vector<int>::iterator i = ratings.begin()+; i!= ratings.end(); i++)
{
// if r[k]>r[k+1]>r[k+2]...>r[k+n],r[k+n]<=r[k+n+1],
// r[i] needs n-(i-k)+(Pre's) candies(k<i<k+n)
// But if possible, we can allocate one candy to the child,
// and with the sequence extends, add the child's candy by one
// until the child's candy reaches that of the prev's.
// Then increase the pre's candy as well. // if r[k] < r[k+1], r[k+1] needs one more candy than r[k]
if(*i < *(i-))
{
//Now we are in a sequence
nSeqLen++;
if(nMaxCntInSeq == nSeqLen)
{
//The first child in the sequence has the same candy as the prev
//The prev should be included in the sequence.
nSeqLen++;
}
nCandyCnt+= nSeqLen;
nPreCanCnt = ;
}
else
{
if(*i > *(i-))
{
nPreCanCnt++;
}
else
{
nPreCanCnt = ;
}
nCandyCnt += nPreCanCnt;
nSeqLen = ;
nMaxCntInSeq = nPreCanCnt;
}
}
}
return nCandyCnt;
}

【leetcode】Candy(hard) 自己做出来了 但别人的更好的更多相关文章

  1. 【leetcode】Best Time to Buy and Sell 3 (hard) 自己做出来了 但别人的更好

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  2. [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现

    [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...

  3. [LeetCode] Candy 分糖果问题

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

  4. [LeetCode] Candy Crush 糖果消消乐

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

  5. leetcode — candy

    /** * Source : https://oj.leetcode.com/problems/candy/ * * There are N children standing in a line. ...

  6. [leetcode]Candy @ Python

    原题地址:https://oj.leetcode.com/problems/candy/ 题意: There are N children standing in a line. Each child ...

  7. Leetcode Candy

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

  8. LeetCode: Candy 解题报告

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

  9. [Leetcode] candy 糖果

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

随机推荐

  1. List<List<double>> lsls = null; 根据double值来重新排序lsls...

    "确定:Node-data = (7,2).具体是:根据x维上的值将数据排序, 6个数据的中值(所谓中值,即中间大小的值)为7, 所以Node-data域位数据点(,).这样, 该节点的分割 ...

  2. express 框架之 路由与中间件

    1.  什么是router路径,什么是middleware? 我们输入www.baidu.com 来访问百度的主页,浏览器会自动转换为 http://www.baidu.com:80/(省略一些参数) ...

  3. iPad 多任务 Spilt View & Size Class

    iPad 多任务 Spilt View & Size Class 一.多任务简介 iOS 9 以后iPad新增了多任务的支持,主要形式有三种: Slide Over (侧边快捷打开) Spil ...

  4. iOS: ARC & MRC下string内存管理策略探究

    ARC & MRC下string内存管理策略探究 前两天跟同事争论一个关于NSString执行copy操作以后是否会发生变化,两个人整了半天,最后写代码验证了一下,发现原来NSString操作 ...

  5. nyoj 613 免费馅饼 广搜

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...

  6. js(jquery)代码在页面上实时地显示时间

    一.引入jquery 二.HTML代码 三.js代码 1)引入js代码 2)下面是完整的js代码

  7. 怎样给WordPress分配更多的内存

    WordPress如果内存不够,你在操作的时候,就会碰到像这样的问题”Allowed memory size of xxxxxx bytes exhausted”(允许的内存 xxxx 字节已经用光了 ...

  8. sql 2005,2008开启bcp的方法嗯哈步骤

    sqlserver 2008开启bcp服务的方法和步骤 sqlserver 2005开启bcp服务的方法和步骤 在开始菜单中找到sql server 2005 -->> 配置工具 --&g ...

  9. webrtc公开课

    http://blog.csdn.net/yangzhenping/article/details/51152376 http://edu.csdn.net/huiyiCourse/live

  10. oracle asm 概念

    automated storage management ,即自动存储管理,简称asm .. 在oracle 10g 这个版本之前,管理一个大型数据库成千上万的数据文件对数据库管理员来说是一个既无技术 ...