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. Hibernate(十)

    1.批处理 //批处理 :批量处理 //批量插入数据 @Test public void addData(){ Random random=new Random(); for(int i=1;i< ...

  2. 使用JavascriptExecutor改变页面元素

    如下如html的页面代码 <html> <body> <input type="text" name="text" value=& ...

  3. yii 验证码 CCaptcha的总结(转)

    今天用到yii的验证码 ccaptcha,经过在网上搜寻 找到以下例子: 1.在controller中加入代码 (1)启用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 &l ...

  4. C# 窗体文件下的 MainForm.cs,MainForm.Designer.cs,MainForm.resx,是什么,干什么

    Form.cs和Form.Designer.cs其实是一个类,Visual Studio为了让我们方便管理,用partial关键字把窗体类给拆开了, Form.Designer.cs存放的是窗体的布局 ...

  5. CentOS 7 上安装(LAMP)服务 Linux,Apache,MySQL,PHP

    介绍 LAMP 是现在非常流行的 WEB 环境, 是 Linux,Apache,MySQL,PHP 的缩写.数据存储在 MySQL 中,动态内容由 PHP 处理. 在本指南中,我们将演示如何在 Cen ...

  6. MVC如何设置启动页

    1.解决方案下的项目,右键,属性,Web,特定页,切换下其他选项以保存

  7. # DZY Love Math 系列

    DZY Love Math 系列 [BOZJ3309] DZY Loves Math 顺着套路就能得到:\(Ans = \sum_{T=1}\lfloor \frac{n}{T} \rfloor \l ...

  8. [UVALive 2678] Subsequence

    图片加载可能有点慢,请跳过题面先看题解,谢谢 在切水题的道路上狂奔,一发不可收拾... 这道题好像不用写什么题解吧,吐个槽什么的算了 一眼题,大佬们都不屑于做,只有我这种弱菜才来写这种题目玩儿 记个前 ...

  9. 胡小兔的NOIP2017游记【出成绩后更新版】

    胡小兔的NOIP2017游记[出成绩后更新版] 2017.11.22 Update 前几天成绩出来啦,看这篇博客访问量还挺多的,下面就分享一下结果吧: 我的Day1T2和Day2T1两道最水的题都跪了 ...

  10. 解题:SDOI 2010 魔法猪学院

    题面 题外话:神**可持久化左偏树,你谷的人都太神了,学不来 我把这个当做A*模板题的说,先讲一讲个人对A*的理解:如果说普通的BFS是Bellman_Ford,那A*就是一个Dijkstra.以寻找 ...