[LeetCode]candy

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?


  1. 开始的时候把题目想简单了,我以为测试数据应该是这样的{1, 3, 4, 2, 4},我一想这不就跟最大子列和一样吗,于是我就设定一个temp值,遇到下一个值大于前一个值就加一,然后sum+=temp,遇到变小则temp置为1.所以分到的糖果数应该是1+2+3+1+2.
  2. 实际是不可行的,因为还有{1, 3, 4, 3, 2, 1}这种情况,按上面的思路4后面的3只分到1个糖果,然而由于3后面还有比3小的2,最终是1+2+3+1+1+1,这样就不符合题意了。我又想,对其正向逆向各扫一遍,逆向的时候temp置为0,这样正向扫完是1+2+3+1+1+1,逆向是0+0+3+2+1+0,但这样4那个位置被加了两次,最终是15,而我们手算可得应该是14,还是不行。
  3. 实在想不出来,最后还是看了戴方勤的LeetCode题解,他把这个temp弄成了数组,也是来回扫,但是只取扫出来的最高值。
class Solution {
public:
    int candy(vector<int>& ratings) {
        int temp = 0,
            n = ratings.size();
        vector<int> temp_arr(n);
        for (int i = 1; i < n; ++i)
        {
            if (ratings[i] > ratings[i - 1])
            {
                temp += 1;
            }
            else
                temp = 0;
            temp_arr[i] = temp;
        }
        temp = 0;
        for (int i = n - 1; i > 0; i--)
        {
            if (ratings[i - 1] > ratings[i])
            {
                temp += 1;
            }
            else
                temp = 0;
            temp_arr[i - 1] = temp > temp_arr[i - 1] ? temp : temp_arr[i - 1];
        }
        return accumulate(&temp_arr[0], &temp_arr[0] + n, n);
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值