leetcode[135] Candy

最少糖果问题。一排小孩,每个孩子有一个优先级,每个孩子至少要发给一个糖果,优先级高的比周围的孩子的糖果要多。

需要注意的是,优先级一样的没有要求说一样多糖果!

先初始化,每人一糖。

为了保证优先级大的比相邻的且优先级小的要糖果多。所以我们分两次处理,一次处理比左边的多,一次处理兼顾左边的多的情况下比右边的多。(如果优先级满足多的前提)

那么第一次从左到右,if (ratings[i] > ratings[i-1]) 那么cd[i] = cd[i-1]+1;    cd指糖果数组

从右边到左边需要注意的是要保证之前的左到右不能减少,所以有一个max取值的问题,也就是需要更改后的如果比原来的值小,那就不用改了,如果改了变大才要改,因为如果改小就不符合大于左边的要求了。即 if(ratings[i] > ratings[i+1]) cd[i] = max(cd[i], cd[i-1]+1)

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

};
时间: 2024-10-18 21:47:36

leetcode[135] Candy的相关文章

LeetCode 135 Candy(贪心算法)

135. 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 mo

LeetCode 135. Candy (O(n)时间 O(n)和O(1)空间的两种实现)

贪心算法,从局部最优推广成全局最优. 这里介绍O(n)时间 O(n)和O(1)空间的两种实现方法. O(n)时间 O(n)空间实现,参考了cnblog, 1957的解法 创建candy数组,初始化为1. 用pre_child_candy记录前一个孩子拿到的糖果数 1. 从左往右遍历 1) 如果ratings[i] > ratings[i-1], 那么candy[i] = ++ pre_child_candy, 亦即这个孩子比前一个孩子多拿一个糖果 2) 反之,candy[i] = pre_chi

leetcode 135. Candy ----- java

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

Java for LeetCode 135 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

【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

【leetcode】Candy(python)

题目要求比其高的邻居要比本身的奖励多,那么最少也要多一个,所有我们可以找到所有的凹点,凹点如下三种情形. 找到所有的凹点后,我们就可以从凹点处开始向左右两个方向依次查找递增序列,其中每个高的都要比相邻的矮的多一个,比如1,2,5,4.我们找到凹点为1 和4,那么从1开始向左没有其他点,我们向右,依次得到2 比1高,2的糖果应该是1的基础上加1,为2, 5比2高,5的糖果是在2的基础上加1,为3.令一个凹点4, 向左,5比4高,5的糖果应该是在4的基础上加 1,为2,这时我们发现冲突了,从凹点1

leetcode -day9 Candy &amp; Gas Station &amp; Binary Tree Maximum Path Sum

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

Baozi Leetcode solution 135: Candy

Problem Statement 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

leetcode || 135、Candy

problem: 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