#leetcode#Candy

本文介绍了一种优化的算法,用于解决在给定儿童评级的情况下,如何分配最少但足够的糖果,确保评级较高的儿童获得比其邻居更多的糖果。通过从左到右和从右到左两次遍历评级数组,算法确保了每个儿童都至少获得一颗糖果,并满足分配规则。最终,通过比较左右两侧的糖果数量,确定每个儿童实际应获得的糖果数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值