题目:

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?

解题思路:

遍历两遍数组即可

第一遍:如果当前元素比前一个大,则当前小孩获得的糖果数为前一个小孩糖果数+1,否则糖果数为1

第二遍:从后往前扫描,如果当前元素i的值大于i+1位置的值,则比较两者目前的糖果数,如果i小孩获得的糖果数大于第i+1个小孩获得糖果数+1,则不变,否则,将i小孩糖果数置为第i+1个小孩糖果数+1.

实现代码:

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class Solution {
  6. public:
  7. int candy(vector<int> &ratings) {
  8. int len = ratings.size();
  9. if(len == || len == )
  10. return len;
  11. int *c = new int[len];
  12. c[] = ;
  13. for(int i = ; i < len; i++)
  14. if(ratings[i] > ratings[i-])
  15. c[i] = c[i-] + ;
  16. else
  17. c[i] = ;
  18. int minCandy = c[len-];
  19. for(int i = len-; i >= ; i--)
  20. {
  21. if(ratings[i] > ratings[i+])
  22. c[i] = max(c[i], c[i+] + );
  23. minCandy += c[i];
  24. }
  25. return minCandy;
  26.  
  27. }
  28. };
  29.  
  30. int main(void)
  31. {
  32. int ratings[] = {,,,,,,};
  33. int len = sizeof(ratings) / sizeof(ratings[]);
  34. vector<int> ratVec(ratings, ratings+len);
  35. Solution solution;
  36. int ret = solution.candy(ratVec);
  37. cout<<ret<<endl;
  38. return ;
  39. }

LeetCode135:Candy的更多相关文章

  1. lc面试准备:Candy

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

  2. LeetCode OJ:Candy(糖果问题)

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

  3. [LeetCode 题解]:Candy

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

  4. 九度OJ 1145:Candy Sharing Game(分享蜡烛游戏) (模拟)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:248 解决:194 题目描述: A number of students sit in a circle facing their teac ...

  5. 转载:Candy? 在线性时间内求出素数与欧拉函数

    转载自:http://www.cnblogs.com/candy99/p/6200660.html 2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB ...

  6. 【树状数组(二叉索引树)】轻院热身—candy、NYOJ-116士兵杀敌(二)

    [概念] 转载连接:树状数组 讲的挺好. 这两题非常的相似,查询区间的累加和.更新结点.Add(x,d) 与 Query(L,R) 的操作 [题目链接:candy] 唉,也是现在才发现这题用了这个知识 ...

  7. 【原创】leetCodeOj --- Candy 解题报告

    题目地址: https://leetcode.com/problems/candy/ 题目内容: Candy Total Accepted: 43150 Total Submissions: 2038 ...

  8. 水题:HDU1034-Candy Sharing Game

    解题心得: 1.我是用的模拟的算法直接模拟的每一轮的分配方法的得出的答案,看到有些大神使用的链表做的,好像链表才是这道题的镇长的做法吧. 题目: Candy Sharing Game Time Lim ...

  9. 模拟:HDU1034-Candy Sharing Game

    解题心得: 1.直接模拟每一次分一半就行了,模拟过程,记录轮数,但是也看到有些大神使用的是链表,估计链表才是真的做法吧. 题目: Candy Sharing Game Time Limit: 2000 ...

随机推荐

  1. poj1661 (DP)

    题目链接:http://poj.org/problem?id=1661 思路: 把初始位置看成左,右端点均为x0,即长度为0,高度为y0的一个平台,按照平台高度从低到高排序.用dp[i][0],dp[ ...

  2. joinablequeue模块 生产者消费者模型 Manager模块 进程池 管道

    一.生产者消费者 主要是为解耦(借助队列来实现生产者消费者模型) import queue  # 不能进行多进程之间的数据传输 (1)from multiprocessing import Queue ...

  3. Linux跑火车,提升趣味性

     實現跑火車[可陶冶情操,愉悦心情]##下载yum源[root@localhost ~]# wget http://mirror.centos.org/centos/7/extras/x86_64/P ...

  4. 安装Python3后,centos使用yum报错

    题记 在之前的文章中我自定义安装了Python3,并且修改了默认的 Python软链,今天想搭建一个 ftp 服务器,使用命令的时候出现了一个错误: 问题 1.使用 yum 安装 ftp工具 yum ...

  5. linux下安装memcached以及扩展(xampp环境)

    网上有很多相关的文章,就不具体写了.(假设这里文件都上传到更目录下的tmp文件夹下) 1.大致流程先装 libevent 和 memcache http://www.cnblogs.com/zgx/a ...

  6. spring中配置监听队列的MQ

    一.spring中配置监听队列的MQ相关信息注:${}是读取propertites文件的常量,这里忽略.绿色部分配置在接收和发送端都要配置.  <bean id="axx" ...

  7. dede5.7 GBK 在php5.4环境下 后台编辑器无法显示文章内容

    问题的原因是:是htmlspecialchars,PHP 5.4后GBK编码下默认不支持中文,转换后内容为空,UTF-8编码没有任何问题.   解决方法如下: 在\include\ckeditor\c ...

  8. Windows10电脑安装macOS Mojave系统的方法(最新版系统,含超详细步骤截图)

    一.环境及准备工作 1.主机系统:本人系统是Windows10家庭中文版 2.虚拟机软件:VMware Workstation 14 Pro 虚拟机版本号:14.1.1 build-7528167 虚 ...

  9. asp.net请求编译流程图(其实就是说asp.netd代码是如何转成中间代码IL然后交给cpu执行的)

  10. 十年前,女:“对不起,我不会喜欢你的,你不要再坚持了,就好比让 Linux 和 Windows 同时运行在一台PC机上,可能吗?

    1.十年前,女:“对不起,我不会喜欢你的,你不要再坚持了,就好比让 Linux 和 Windows 同时运行在一台PC机上,可能吗?”男生听后默默走开, 十年后,在一次虚拟技术大会上,我听到一名虚拟技 ...