[LeetCode]Candy
2016-12-06 19:49
330 查看
Question
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?
本题难度Hard。
贪心法
【复杂度】
时间 O(N) 空间 O(N)
【思路】
典型的贪心法,如果一个孩子比另一个孩子的分高,我们只多给1块糖。我们可以先从左往右遍历,确保每个孩子根他左边的孩子相比,如果分高,则糖要多1个,如果分比左边低,就只给一颗。然后我们再从右往左遍历,确保每个孩子跟他右边的孩子相比,如果分高则糖至少多1个(这里至少多1个的意思是,我们要取当前孩子手里糖的数量,和其右边孩子糖的数量加1,两者的较大值)。
【代码】
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?
本题难度Hard。
贪心法
【复杂度】
时间 O(N) 空间 O(N)
【思路】
典型的贪心法,如果一个孩子比另一个孩子的分高,我们只多给1块糖。我们可以先从左往右遍历,确保每个孩子根他左边的孩子相比,如果分高,则糖要多1个,如果分比左边低,就只给一颗。然后我们再从右往左遍历,确保每个孩子跟他右边的孩子相比,如果分高则糖至少多1个(这里至少多1个的意思是,我们要取当前孩子手里糖的数量,和其右边孩子糖的数量加1,两者的较大值)。
【代码】
public class Solution { public int candy(int[] ratings) { //require int size=ratings.length; if(size<2)return size; int[] f=new int[size]; f[0]=1; //invariant // 先从左往右分糖,分数较高的多拿一颗糖,分数较少的只拿一颗糖 for(int i=1;i<size;i++){ if(ratings[i-1]<ratings[i]){ f[i]=f[i-1]+1; }else f[i]=1; } int sum=f[size-1]; // 再从右往左继续分糖,分数较高的确保比右边多一颗就行了 for(int i=size-2;i>=0;i--){ if(ratings[i+1]<ratings[i]) f[i]=Math.max(f[i+1]+1,f[i]); sum+=f[i]; } //ensure return sum; } }
参考
[Leetcode] Candy 分糖果相关文章推荐
- LeetCode Candy
- LeetCode-Candy
- LeetCode "Candy" - from skill to thought
- [leetcode]Candy
- [LeetCode] Candy
- leetCode解题报告之Candy(简单回溯)
- leetcode Candy
- [LeetCode] Candy
- 【LeetCode】Candy
- leetcode 138: Candy (incompleted)
- [LeetCode]Candy, 解题报告
- LeetCode || Candy
- 【leetcode】Candy
- Candy [leetcode] O(n)时间复杂度,O(1)空间复杂度的方法
- LeetCode: Candy
- [LeetCode] Candy
- 4.19 leetcode -19 candy
- 【leetcode】Candy(python)
- leetcode :Candy
- [LeetCode]Candy