题目:

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. Go语言中cannot convert adminname (type interface {}) to type *: need type assertion的解决办法

    解决的办法是把string(adminname)替换为adminname.(string).其它类型也是类似.

  2. oracle存储过程-获取错误信息

    dbms_output.put_line('code:' || sqlcode); dbms_output.put_line('errm:' || sqlerrm); dbms_output.put_ ...

  3. Java的反射和代理以及注解

    最近接触到java的反射和代理(接触的有点迟了...),还是有必要总结下 1. Java的反射 有的时候我们需要在程序运行的时候获取类.方法等信息用于动态运行,这个时候反射就能够帮我们找到类.方法.成 ...

  4. BeanFactory 和 ApplicationContext的区别

    今天在网上查资料无意中看到这一行代码 BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext ...

  5. BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource

    Tomcat报错如下: BeanDefinitionStoreException: IOException parsing XML document from ServletContext resou ...

  6. 内部存储 openFileInputStream openFileOutputStream

    package com.qianfeng.gp08_day24_internalstorage; import java.io.FileInputStream; import java.io.File ...

  7. 让php支持多线程,win下安装pthreads

    1.检查PHP版本是否支持线程安全 在phpinfo()的显示页中,搜索Thread Safety,如果是enabled,则PHP版本是线程安全的. 2.在http://windows.php.net ...

  8. Android 获取ROOT权限原理解析

    一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android玩家中常说的“越狱”有一个更深层次的认识. 二. Root的介绍 1.       Root 的目的 可以让 ...

  9. 我的UI启蒙之路

    为什么叫UI启蒙之路呢? 我没有学过美术,也不懂设计,但是有的时候也许就是一种命中注定吧,让我知道了UI,并且一发不可收拾的爱上了它. 具体情况是这样的: 我毕业于电力学校,是一名不折不扣的工科生,专 ...

  10. 交互神器-最好用的Mac原型设计工具

    市场上有着大量的开发和设计工具支持在Mac上安装使用,今天给大家强烈推荐一款Mac上的原型设计工具-Mockplus,原型工具在产品开发设计中是必不可少的,无论是现在非常火的小程序设计,还是网页设计, ...