LeetCode 135 Candy(贪心算法)
135. Candy
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?
要求使用最少的糖分给不同孩子,要注意只是有更高分数的拿更多的糖,相同分数可以给不同数量的糖。
考虑对第 i 个孩子,如果他的分数等于之前的孩子,那给他一颗糖就能满足条件。如果他的分数大于之前的孩子,给他前一个孩子的糖+1。如果小于,考虑给一颗糖,如果上一个孩子有不止一颗糖,可以满足条件,但是如果上一个孩子只有一颗糖,对上一个孩子来说,他有比右边孩子更高的分数,但是只有相同的糖的数目。所以这种情况需要重新考虑。
一个方法是对 i 之前的孩子遍历,依次增多糖的数目直到满足条件,但是这样复杂度达到O(n^2),后面的case会超时。所以只能改变思路,一旦发现这样的 i ,我们从 i 开始找到分数最长连续递减的序列。比如 [1,4,3,2,1] 里,遍历到 2 时,糖果分配情况是
1,2,1 我们从 2 开始 找到递减序列 2,1 然后从 1 开始倒着对少分糖的孩子发糖,得到正确的分糖情况 [1,4,3,2,1] 。最后考虑一下边界即可。
python AC代码:
class Solution(object):
def candy(self, ratings):
can = [1 for _ in ratings]
i = 1
while i<len(ratings):
if ratings[i]==ratings[i-1]:
i+=1
elif ratings[i]>ratings[i-1]:
can[i]=can[i-1]+1
i+=1
else:
if can[i-1]==1:
while i+1<len(ratings) and ratings[i+1]<ratings[i]:
i+=1
j = i
while j-1>=0 and ratings[j-1]>ratings[j] and can[j-1]<=can[j]:
can[j-1]=1+can[j]
j-=1
if j>0 and ratings[j-1]<ratings[j] and can[j-1]>=can[j]:
can[j] = can[j-1]+1
else:
i+=1
return sum(can)
LeetCode 135 Candy(贪心算法)的更多相关文章
- LeetCode解题记录(贪心算法)(二)
1. 前言 由于后面还有很多题型要写,贪心算法目前可能就到此为止了,上一篇博客的地址为 LeetCode解题记录(贪心算法)(一) 下面正式开始我们的刷题之旅 2. 贪心 763. 划分字母区间(中等 ...
- C#LeetCode刷题-贪心算法
贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配 17.8% 困难 45 跳跃游戏 II 25.5% 困难 55 跳跃游戏 30.6% 中等 122 买卖股票的最佳时机 II C ...
- LeetCode解题记录(贪心算法)(一)
1. 前言 目前得到一本不错的算法书籍,页数不多,挺符合我的需要,于是正好借这个机会来好好的系统的刷一下算法题,一来呢,是可以给部分同学提供解题思路,和一些自己的思考,二来呢,我也可以在需要复习的时候 ...
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
- leetcode 135. Candy ----- java
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Leetcode#135 Candy
原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...
- Java for LeetCode 135 Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- [leetcode] 135. Candy (hard)
原题 前后两遍遍历 class Solution { public: int candy(vector<int> &ratings) { vector<int> res ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
随机推荐
- 如何在Jenkins CI 里调试
背景 厂内的CI系统把 Jenkins 和Github 连接了起来,这样Dev 只要通过github pr 就能够了解到测试job 运行的情况.有的时候,Dev会找到QA问,如何在Jenkins CI ...
- UIScrollView在AutoLayout下的滚动问题
使用Storyboard编写UI,设置支持AutoLayout. 在其中的一个场景上,添加一个UIScrollView,在对应的代码里增加 - (void)viewDidLoad { [super v ...
- oracle创建删除用户和表空间
创建用户:sqlplus /nologconn / as sysdba;create user username identified by passwordgreant dba to usernam ...
- LeetCode---Binary Search
475. Heaters 思路:每趟循环查找离房子最近的热水器,计算距离,最后取最大距离 public int findRadius(int[] houses, int[] heaters) { Ar ...
- 为了方便可灌入自定义方法AppendLog 比如File
说起到qt的编译,真是领人痛心啊,不仅编译选项繁多,而且编译时间比较久,总是能使想编译qt源码的人望而却步,呵呵...我就是其中一个,不知道从什么时候开始就想着把qt的源码编译一下,也尝试过几次,但都 ...
- MSER算法介绍
MSER代码编译: matlabroot %如果是VS2010则解压VS2010MEX支持文件到MATLAB根目录 unzip('E:\Software\develop Tools\VS2010MEX ...
- 在CentOS上安装并运行SparkR
环境配置—— 操作系统:CentOS 6.5 JDK版本:1.7.0_67 Hadoop集群版本:CDH 5.3.0 安装过程—— 1.安装R yum install -y R 2.安装curl-de ...
- SQL 导出表结构到Excel
SQL 导出表结构到Excel SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号 = a ...
- LeetCode "Is Subsequence"
There are 3 possible approaches: DP, divide&conquer and greedy. And apparently, DP has O(n^2) co ...
- 循环语句--for
1.guess_age优化版v1.py #coding=utf-8 age = 22 for i in range(10): if i < 3: guess_num = int(input('i ...