LintCode 412: Candy

题目描述

N 个小孩站成一列。每个小孩有一个评级。

按照以下要求,给小孩分糖果:

  • 每个小孩至少得到一颗糖果。

  • 评级越高的小孩可以得到更多的糖果。

需最少准备多少糖果?

样例

给定评级 = [1, 2], 返回 3.

给定评级 = [1, 1, 1], 返回 3.

给定评级 = [1, 2, 2], 返回 4. ([1,2,1]).

Sat Feb 26 2017

思路

首先题目中的第三个样例一开始我想不明白为什么是那样,后来才发现,评级比两旁的小孩高的话,糖果肯定要比旁边的小孩多,但是评级与两旁的小孩一样的话,是可以比他们少的。

其次本题只需要从前往后以及从后往前遍历两次即可,只需要保证评级高得到多一个糖果,不需要保证评级相同也得到相同的糖果。

代码

// 分糖果
int candy(vector<int>& ratings)
{
int n = ratings.size(), ans = 0;;
vector<int> nums(n, 1);
for (int i = 0; i < n; ++i)
{
if (i > 0 && ratings[i] > ratings[i - 1] && nums[i] <= nums[i - 1])
nums[i] = nums[i - 1] + 1;
if (i + 1 < n && ratings[i] > ratings[i + 1] && nums[i] <= nums[i + 1])
nums[i] = nums[i + 1] + 1;
}
for (int i = n - 1; i >= 0; --i)
{
if (i > 0 && ratings[i] > ratings[i - 1] && nums[i] <= nums[i - 1])
nums[i] = nums[i - 1] + 1;
if (i + 1 < n && ratings[i] > ratings[i + 1] && nums[i] <= nums[i + 1])
nums[i] = nums[i + 1] + 1;
ans += nums[i];
}
return ans;
}

LintCode 412: Candy的更多相关文章

  1. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  2. [LintCode] Candy 分糖果问题

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

  3. [LeetCode] Candy Crush 糖果消消乐

    This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...

  4. [LeetCode] 723. Candy Crush 糖果消消乐

    This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...

  5. LeetCode 723. Candy Crush

    原题链接在这里:https://leetcode.com/problems/candy-crush/ 题目: This question is about implementing a basic e ...

  6. [LeetCode] 723. Candy Crush 糖果粉碎

    This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...

  7. 【LeetCode】723. Candy Crush 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...

  8. [LeetCode] Candy 分糖果问题

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

  9. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

随机推荐

  1. asp.net简述WP开发模式

    详情请参考菜鸟教程:http://www.runoob.com/aspnet/aspnet-tutorial.html 1.ASP.NET 是一个使用 HTML.CSS.JavaScript 和服务器 ...

  2. UDJC用户自定义Java类

    private RowSet t1 = null;//业务表步骤 private RowSet t2 = null;//删除步骤 public boolean processRow(StepMetaI ...

  3. 微信小程序 功能函数 替换字符串内的指定字符

    var str = 'abcadeacf'; var str1 = str.replace('a', 'o'); alert(str1);    // 打印结果: obcadeacf   var st ...

  4. 数据库事务的四大特性以及4种事务的隔离级别-以及对应的5种JDBC事务隔离级别

    本篇讲诉数据库中事务的四大特性(ACID),并且将会详细地说明事务的隔离级别. 如果一个数据库声称支持事务的操作,那么该数据库必须要具备以下四个特性: ⑴ 原子性(Atomicity) 原子性是指事务 ...

  5. command symbol & mac & emoji

    command symbol & mac & emoji how to input command symbol in mac ? https://apple.stackexchang ...

  6. Scrapy初尝试

    ,python3.6版本 在看网上的安装的时候下一堆依赖,其实没有必要一个个的去装,pip直接分析依赖一块下载安装下来! 已经安装了pip模块 直接上 pip install scrapy 安装twi ...

  7. mysql中LIKE和REGEXP

    mysql中LIKE和REGEXP都可以用来字符匹配 正则表达式REGEXP是为复杂搜索指定模式的强大方式. like用法   LIKE一般与通配符(%)和(_)两个使用 如例 SELECT prod ...

  8. [CF1110H]Modest Substrings

    description CodeForces 定义一个正整数\(x\)是合适的当且仅当\(l\le x\le r\),其中\(l,r\le 10^{800}\). 找到一个长度为\(n\)的数字串,使 ...

  9. Android Log详解(Log.v,Log.d,Log.i,Log.w,Log.e)

    1.Log.v 的调试颜色为黑色的,任何消息都会输出,这里的v代表verbose啰嗦的意思,平时使用就是Log.v("",""); 2.Log.d的输出颜色是蓝 ...

  10. 解题:POI 2007 Tourist Attractions

    题面 事实上这份代码在洛谷过不去,因为好像要用到一些压缩空间的技巧,我并不想(hui)写(捂脸) 先预处理$1$到$k+1$这些点之间相互的最短路和它们到终点的最短路,并记录下每个点能够转移到时的状态 ...