[leetcode]135. Candy 分发糖果

链接:https://leetcode.com/problems/candy/description/

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?

Example 1:

Input: [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.

Example 2:

Input: [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
             The third child gets 1 candy because it satisfies the above two conditions.

we can make use of a single array candiescandies to keep the count of the number of candies to be allocated to the current student. In order to do so, firstly we assign 1 candy to each student. Then, we traverse the array from left-to-right and distribute the candies following only the left neighbour relation i.e. whenever the current element's ratings is larger than the left neighbour and has less than or equal candies than its left neighbour, we update the current element's candies in the candiescandies array as candies[i] = candies[i-1] + 1candies[i]=candies[i−1]+1. While updating we need not compare candies[i]candies[i] and candies[i - 1]candies[i−1], since candies[i] \leq candies[i - 1]candies[i]≤candies[i−1] before updation. After this, we traverse the array from right-to-left. Now, we need to update the i^{th}ith​​ element's candies in order to satisfy both the left neighbour and the right neighbour relation. Now, during the backward traversal, if ratings[i]>ratings[i + 1]ratings[i]>ratings[i+1], considering only the right neighbour criteria, we could've updated candies[i]candies[i] as candies[i] = candies[i + 1] + 1candies[i]=candies[i+1]+1. But, this time we need to update the candies[i]candies[i] only if candies[i] \leq candies[i + 1]candies[i]≤candies[i+1]. This happens because, this time we've already altered the candiescandies array during the forward traversal and thus candies[i]candies[i] isn't necessarily less than or equal to candies[i + 1]candies[i+1]. Thus, if ratings[i] > ratings[i + 1]ratings[i]>ratings[i+1], we can update candies[i]candies[i] as candies[i] = \text{max}(candies[i], candies[i + 1] + 1)candies[i]=max(candies[i],candies[i+1]+1), which makes candies[i]candies[i] satisfy both the left neighbour and the right neighbour criteria.

Again, we need sum up all the elements of the candiescandies array to obtain the required result.

class Solution {
public:
    int candy(vector<int>& ratings) {
        vector<int> candies(ratings.size(),1);
        
        for(int i=1;i<ratings.size();i++)
        {
            if(ratings[i]>ratings[i-1])
            {
                candies[i]=candies[i-1]+1;
            }
        }
        int sum=candies[ratings.size()-1];
        for(int i=ratings.size()-2;i>=0;i--)
        {
            if(ratings[i]>ratings[i+1])
            {
                candies[i]=max(candies[i],candies[i+1]+1);
            }
            sum+=candies[i];
        }
        return sum;
    }
};

minimum_candies = i =0 n 1 c a n d i e s [ i ] ,where  n =length of the ratings array.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值