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. 使用MVVM框架(avalonJS)进行快速开发

    背景 在运营活动开发中,因为工作的重复性很大,同时往往开发时间短,某些情况下也会非常紧急,导致了活动开发时间被大大压缩,同时有些活动逻辑复杂,数据或者状态变更都需要手动渲染,容易出错,正是因为这些问题 ...

  2. height:100%和height:auto的区别

    一直不明白height:100%和height:auto的区别,最近在制作前端页面时都用了height:100%;overflow:hidden; ,可是有些浏览器出现莫名的奇妙的问题,但换成heig ...

  3. HDU 5047 Sawtooth(大数模拟)上海赛区网赛1006

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047 解题报告:问一个“M”型可以把一个矩形的平面最多分割成多少块. 输入是有n个“M",现 ...

  4. iOS开发——UI进阶篇(十)导航控制器、微博详情页、控制器的View的生命周期

    一.导航控制器出栈 1.initWithRootViewController本质 UIViewController *vc = [[OneViewController alloc] init]; // ...

  5. style="visibility: hidden" 和 style=“display:none”区别

    大多数人很容易将CSS属性display和visibility混淆,它们看似没有什么不同,其实它们的差别却是很大的. visibility属性用来确定元素是显示还是隐藏的,这用visibility=& ...

  6. (七)理解angular中的module和injector,即依赖注入

    (七)理解angular中的module和injector,即依赖注入 时间:2014-10-10 01:16:54      阅读:63060      评论:1      收藏:0      [点 ...

  7. 将kindeditor在线编辑器制作成smarty插件

    在web开发中,在线编辑器是经常要用到的功能模块,虽说配置在线编辑器没有多大难度,但是每一次编写重复的代码,总是让人感觉不爽. 本篇,就来讲解一下,如何将kindeditor制作成smarty的一个自 ...

  8. YUVviewerPlus使用教程

    1.YUVviewerPlus用于播放yuv文件,点击Open File打开yuv文件 2.点击Play播放yuv文件

  9. iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用

    http://www.mamicode.com/info-detail-514151.html 由于Xcode6之后,默认创建storyboard而非xib文件,而作为初学,了解xib的加载原理很重要 ...

  10. spring3 的restful API RequestMapping介绍

    原文链接:http://www.javaarch.net/jiagoushi/694.htm spring3 的restful API RequestMapping介绍 在spring mvc中 @R ...