题目:

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. Sql求和异常——对象不能从 DBNull 转换为其他类型

    做项目遇到一个以前没遇到的问题,就是要计算一个用户消费总额, 关键代码如下: string sql = "select sum(Tmoney) from [order] where uid= ...

  2. ftp使用(图文详解)

    41,准备: FileZilla_Server-v0.9.41.rar 2.安装,可以全部默认下一步 这里装在了E盘 3安装成功添加用户 添加用户名yanan 选择share folders选择要共享 ...

  3. curl学习(实例不断总结)

    1.先来一个简单的案例,请求http协议的网站 // 初始化一个 cURL 对象 $curl = curl_init(); // 设置你需要抓取的URL curl_setopt($curl, CURL ...

  4. Spring AOP配置

    相关概念有点拗口,我这里简单总结一个,切面,决定做什么,写处理逻辑,比如打日志.切入点,决定在哪些方里拦截,一般填正则表达式查询. 通知,就是连接切面和切入点的桥梁. 其中遇到了配置好,启动服务器没报 ...

  5. 2018.06.29 NOIP模拟 繁星(前缀和)

    繁星 [问题描述] 要过六一了,大川正在绞尽脑汁想送给小伙伴什么礼物呢.突然想起以前拍过一张夜空中的繁星的照片,这张照片已经被处理成黑白的,也就是说,每个像素只可能是两个颜色之一,白或黑.像素(x,y ...

  6. 2018.09.11 loj#10216.五指山(exgcd)

    传送门 就是一个exgcd的板子. 但注意算距离差的时候是在一个环上面算. 还有,答案要开long long233... 注意这两点之后就是exgcd板子了. 代码: #include<bits ...

  7. 2018.09.08 bzoj4518: [Sdoi2016]征途(斜率优化dp)

    传送门 把式子展开后发现就是要求: m∗(∑i=1msum′[i])−sum[n]2" role="presentation" style="position: ...

  8. 2018.08.16 洛谷P2029 跳舞(线性dp)

    传送门 简单的线性dp" role="presentation" style="position: relative;">dpdp. 直接推一推 ...

  9. 搭建Idea授权服务器用于学习

    我自己的搭建服务器http://doit.wenyule.top 懒得看教程或弄不好的小伙伴可以用我搭建的,在激活那选择服务器,输入我上面的地址,注意可以激活2018.2.1之前的.为了防止用的人太多 ...

  10. UVa 10881 Piotr's Ants (等价变换)

    题意:一个长度为L的木棍上有n个蚂蚁,每只蚂蚁要么向左,要么向右,速度为1,当两只蚂蚁相撞时, 它们同时掉头.给定每只蚂蚁初始位置和朝向,问T秒后,每只蚂蚁的状态. 析:刚看到这个题时,一点思路也没有 ...