LintCode_412 Candy
来源:互联网 发布:北京java薪资待遇 编辑:程序博客网 时间:2024/06/17 16:52
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?
Example
Given ratings = [1, 2]
, return 3
.
Given ratings = [1, 1, 1]
, return 3
.
Given ratings = [1, 2, 2]
, return 4
. ([1,2,1]).
分析:其实题目描述的有点问题,不是评级越高的小孩可以得到更多的糖果,从第三个样例中可以看出来,应该是“相邻两人里评级高的,可以得到更多的糖果”,一开始可以假设每个小孩都1颗糖果,如果第i个小孩比i-1个小孩rating高,那么糖果数也得比他高,和i+1个小孩比也是这样,于是可以从前往后扫描一遍,再从后往前扫描一遍。
0 0
- LintCode_412 Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- candy
- Candy
- Candy
- Candy
- Android中利用newInstance()方法实例化fragment
- ambiq micro 的滴答定时器使用
- Angular ng 学习
- 企业如何搭建数据分析平台
- 数据库分页的方式
- LintCode_412 Candy
- Linux进程线程学习笔记:进程间通信之 管道
- ORA-00932:数据类型不一致:应为-,但却获得CLOB
- CSS双飞翼布局实例
- Cash Machine--多重背包
- 【NOIP2016提高A组模拟8.15】Password
- 解剖Nginx·模块开发篇(5)解读内置非默认模块 ngx_http_stub_status_module
- FluentPDO备忘
- JavaScript强化教程——jQuery UI API 类别