#leetcode#Candy
2015-06-11 10:38
399 查看
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大神
这里粘贴一段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
From left to right, to make all candies satisfy the condition
a solution which is satisfied.
From right to left, almost like step 1, get a solution
just satisfy the condition
For now, we have
so how can we satisfy the real condition in the problem? Just make
the maximum between
Here are something to notice:
Set all
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
it just help to explain my thought. So there are only a
it is O(N) space complexity.
Here is a sample:
ratings:
in step 1, go from left to right, you can get leftCandies
in step 2, go from right to left, you can get rightCandies
in step 3, you can get the real candies
get the answer.
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 assume
leftCandies[i]is
a solution which is satisfied.
From right to left, almost like step 1, get a solution
rightCandies[i]which
just satisfy the condition
if ratings[i] > ratings[i + 1] then candies[i] > candies[i + 1]
For now, we have
leftCandies[i]and
rightCandies[i],
so how can we satisfy the real condition in the problem? Just make
candies[i]equals
the maximum between
leftCanides[i]and
rightCandies[i]
Here are something to notice:
Set all
leftCandies[i]
rightCandies[i]to
1at
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 a
candies[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 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解
- LeetCode [Day 5] Longest Palindromic Substring 题解
- LeetCode [Day 6] ZigZag Conversion 题解
- LeetCode 2: Add Two Numbers (JAVA)
- LeetCode 1: Two Sum (JAVA)