LintCode 412: Candy
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;
}
转载于:https://www.cnblogs.com/genkun/p/6444981.html
LintCode 412: Candy相关推荐
- [LeetCode]135.Candy
[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【LeetCode】723. Candy Crush 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...
- LeetCode之Candy
[题目] There areNchildren standing in a line. Each child is assigned a rating value. You are giving ca ...
- LintCode 249. 统计前面比自己小的数的个数
给定一个整数数组(下标由 0 到 n-1, n 表示数组的规模,取值范围由 0 到10000).对于数组中的每个 ai 元素,请计算 ai 前的数中比它小的元素的数量. 注意事项 We suggest ...
- LintCode: Max Tree
题目 Given an integer array with no duplicates. A max tree building on this array is defined as follow ...
- 【LintCode: 3. 统计数字】算法题解析
这是一道来自LintCode的算法题目,本文用C++来解答这道题,链接为: https://www.lintcode.com/problem/digit-counts/description 题目描述 ...
- LintCode 1.A+B的问题
LintCode 1.A+B的问题 描述 给出两个整数 a 和 b , 求他们的和. 答案 public class Solution {/*** @param a: An integer* @par ...
- 关于爬楼梯的lintcode代码
讲真的,这个我只会用递归去做,但是lintcode上面超时,所以只有在网上找了个动态规划的,虽然这个程序懂了,但是我觉得还是挺不容易的真正弄懂的话-- class Solution { public: ...
- lintcode: k Sum 解题报告
K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...
- php 7.0 curl图片上传,PHP 升级到7.4.0后,通过curl上传文件http_code报412
我将PHP环境升级到7.4.0后,使用curl向微信公众平台上传图片不能成功,代码如下所示,切换到php7.3.x版本运行正常.php7.4.0版本下,在http_post方法的倒数第二行打印相关信息 ...
最新文章
- 1130 Infix Expression
- python系统-Python OS模块常用功能 中文图文详解
- CKEditor代码高亮显示插件Code Snippet安装及使用方法
- 你知道铅酸蓄电池的常见失效模式吗?
- 恐鬼症不显示服务器区域,恐鬼症 steam版【简体中文】【4.34GB】
- 【Hibernate3.3复习知识点二】 - 配置hibernate环境(annotations)
- 平果手机桌面计算机,苹果手机便签记事本怎么在Windows电脑桌面上使用?
- 使用Python模拟男人在长椅上选择座位的过程
- 用javascript+PHP随机显示图片
- Ubuntu14.04桌面版 apt-get 方式安装LNMP
- Keil v5安装和使用
- python菜鸟教程 | 字典(Dictionary)
- 三维空间点到线段的距离
- Android 扫描二维码、制作二维码、识别图片中的二维码;简单的Zxing扫一扫功能;
- 想要挣钱创收 那就用脚本操作手机群控软件啊
- wps删除第二页页眉
- TeamViewer安全证书过期,解决办法
- 可视化数据库监控警报工具 —— Sampler
- Java3:数组和字符串
- JAVA Signal Handing
热门文章
- 写偏斜(Write Skew)和丢失更新(Lost Updates)区别
- PHP ceil函数bug错误的解决方案
- Spring学习(一)Spring简介、SpringIOC
- 远程计算机统考试题,2015年电大远程网络教育计算机应用基础统考模拟试题集...
- java guava map创建,数组合并,数组分割连接
- Canvas beginPath()、moveTo()、 lineTo() 、stroke() 绘制直线路径,fillText() 绘制填色的文本
- php 获取array keys,php数组函数序列之
- 涿州有没有java_谁说涿州没有方言?
- [SHOI2008]堵塞的交通
- kali 32位 更换 xfce4 桌面