#leetcode#Candy
来源:互联网 发布:淘宝仅退款不退货规则 编辑:程序博客网 时间:2024/06/05 18:21
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?
举个例子: ratings[] = new int[]{2, 10, 1, 2, 4, 1, 2}
这里注意题目要求rating高的child比他的neighbors高就可以了!!!所以只比较左右邻居, 不用全局比较!!!
解题思路依旧code ganker大神
public class Solution { public int candy(int[] ratings) { if(ratings == null || ratings.length == 0){ return 0; } int[] nums = new int[ratings.length]; nums[0] = 1; for(int i = 1; i < ratings.length; i++){ if(ratings[i] > ratings[i - 1]){ nums[i] = nums[i - 1] + 1; }else{ nums[i] = 1; } } int res = nums[ratings.length - 1]; for(int i = ratings.length - 2; i >= 0; i--){ int cur = 0; if(ratings[i] > ratings[i + 1]){ cur = nums[i + 1] + 1; }else{ cur = 1; } res += Math.max(nums[i], cur); nums[i] = cur; } return res; }}
这里粘贴一段leetcode discuss的解释
Yes, the condition considering in your solution looks complicated. And I can hardly understand it.
Here I provide my solution for your refering. Same as you, time complexity is exactly O(N) not only the average time cost, and O(N) space needed.
I also go thru the ratings array two times, from left to right and from right to left.
Here are several steps of my algorithm:
Assume candies[i]
means count of candies give to child i
.
From left to right, to make all candies satisfy the condition
if ratings[i] > ratings[i - 1] then candies[i] > candies[i - 1]
, just ignore the right neighbor as this moment. We can assumeleftCandies[i]
is a solution which is satisfied.From right to left, almost like step 1, get a solution
rightCandies[i]
which just satisfy the conditionif ratings[i] > ratings[i + 1] then candies[i] > candies[i + 1]
For now, we have
leftCandies[i]
andrightCandies[i]
, so how can we satisfy the real condition in the problem? Just makecandies[i]
equals the maximum betweenleftCanides[i]
andrightCandies[i]
Here are something to notice:
- Set all
leftCandies[i]
rightCandies[i]
to1
at the beginning of going thru, since each child need at least one candy. - When you implement the algorithm, you do not need the real array
leftCandies
rightCandies
, it just help to explain my thought. So there are only acandies[i]
needed. However, it is O(N) space complexity.
Here is a sample:
- ratings:
[1,5,3,1]
- in step 1, go from left to right, you can get leftCandies
[1,2,1,1]
- in step 2, go from right to left, you can get rightCandies
[1,3,2,1]
- in step 3, you can get the real candies
[1,3,2,1]
, just sum it then get the answer.
- Leetcode: Candy
- LeetCode:Candy
- [leetcode]Candy
- Leetcode: Candy
- 【leetcode】Candy
- LeetCode:Candy
- [LeetCode] Candy
- [LeetCode]Candy
- [LeetCode] Candy
- 【LeetCode】Candy
- LeetCode | Candy
- Leetcode: Candy
- Candy - LeetCode
- leetcode Candy
- LeetCode Candy
- candy leetcode
- Candy -- LeetCode
- [LeetCode] Candy
- LeetCode 之 Longest Common Prefix — C++实现
- leetcode sqrt
- java 并发编程 AbstractQueuedSynchronizer 加锁和解锁分析
- iOS 分享功能开发
- sshutil工具介绍:一个跨机器的脚本执行器
- #leetcode#Candy
- IBM---Java 多线程与并发编程专题
- 【Leetcode Algorithm】Roman to Integer
- IM消息的显示方法和装置-专利
- 总结:接口、类、抽象类、继承、构造函数、方法重写、方法重载、自动转型、多态、引用传递
- Android程序:使用SeekBar实现滑动进度条功能
- c# lock
- Linux线程-互斥锁pthread_mutex_t
- Expression is not assignable