#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.

  1. 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.

  2. 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]

  3. 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 betweenleftCanides[i] and rightCandies[i]

Here are something to notice:

  • Set all leftCandies[i] rightCandies[i] to 1 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 leftCandiesrightCandies, 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.

0 0
原创粉丝点击