题目:

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.

实现代码:

#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
int candy(vector<int> &ratings) {
int len = ratings.size();
if(len == || len == )
return len;
int *c = new int[len];
c[] = ;
for(int i = ; i < len; i++)
if(ratings[i] > ratings[i-])
c[i] = c[i-] + ;
else
c[i] = ;
int minCandy = c[len-];
for(int i = len-; i >= ; i--)
{
if(ratings[i] > ratings[i+])
c[i] = max(c[i], c[i+] + );
minCandy += c[i];
}
return minCandy; }
}; int main(void)
{
int ratings[] = {,,,,,,};
int len = sizeof(ratings) / sizeof(ratings[]);
vector<int> ratVec(ratings, ratings+len);
Solution solution;
int ret = solution.candy(ratVec);
cout<<ret<<endl;
return ;
}

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. web项目生成web.xml的两种方式

    做了很多的项目,今天着手写个小demo发现做web项目的时候还需要从别的地方去拷贝,那么如果没有地方可以拷贝,要怎么办呢?下边介绍三种方式生成web.xml文件. 一.maven项目情况:(STS版) ...

  2. ubuntu安装谷歌拼音输入法

    在这篇教程中,我将告诉你如何在ubuntu系统上安装谷歌拼音输入法.谷歌拼音输入法有基于ibus框架的,也有基于fcitx框架的.我只演示fcitx框架下谷歌拼音输入法的安装,因为ibus框架的谷歌拼 ...

  3. ftp指令集

    想进行ftp相关编程,得了解ftp的指令集. 可参考:http://blog.csdn.net/weiyuefei/article/details/51758288 ftp的命令和指令集不同: 命令是 ...

  4. python 网络客户端编程端口,模块

    协议 功能 端口 模块 HTTP 网页 80 httplib,urllib,xmlrpclib NNTP Usenet 新闻组 119 nntplib FTP 文件传输 20(21控制和命令端口) f ...

  5. python作业之生成随机数

    作业要求 生成一个6个字符长度的随机数,要求是包括字母和数字的组合 import random l1 = [] for i in range(6): a = random.randrange(0,10 ...

  6. Python requests 使用心得

    最近在用requests写一些项目,遇见了一些问题,百度了很多,有些都不太好使,最后看了下requestsAPI文档,才明白了很多,最后项目趋于稳定.看来学东西还是API文档比较权威啊~ 问题场景 项 ...

  7. FutureTask详解

    1 基本概念 1.1 Callable与Future Runnable封装一个异步运行的任务,可以把它想象成为一个没有参数和返回值的异步方法.Callable与Runnable类似,但是有返回值.Ca ...

  8. linux下给php安装memcached及memcache扩展(转)

    http://kimi.it/257.html (另外的方法)linux安装memcached及memcache扩展一.安装libevent函数库下载地址:http://libevent.org默认被 ...

  9. 2017年值得一看的7个APP设计

    新媒体时代蓬勃发展,各类APP如雨后春笋般出现.下载到合适的APP,不仅衣食住行一键搞定,甚至健身.社交.阅读等需求也能足不出户地满足.对于广大“吃瓜群众”来说,选择APP是个人需求以及跟随潮流的选择 ...

  10. 2018上IEC计算机高级语言(C)作业 第0次作业

    最理想的师生关系是健身教练和学员的关系,在这种师生关系中你期望获得来自老师的哪些帮助? 最理想的的师生关系是健身教练和学员的关系,其实我个人感觉不太认同,我觉得老师和学生之间更多的是一种共生关系,像植 ...