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 than their neighbors.
What is the minimum candies you must give?

详见:https://leetcode.com/problems/candy/description/

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

参考:https://www.cnblogs.com/grandyang/p/4575026.html

原文地址:https://www.cnblogs.com/xidian2014/p/8724631.html

时间: 2024-08-01 23:15:15

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] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现

[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: 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

欢乐暑假线上编程比赛第四题:分配糖果

在csdn上看到这么个题目,与友友们一起分享下,如果有别的做法,也希望能拿出来交流交流. 题目详情 有n个小朋友站成一排(编号从0到n-1),每个小朋友有一个rating值,存放在ratings数组中.老师需要给他们分 配糖果,每个小朋友至少需要一颗糖果,对于任意相邻的两个小朋友i和i+1,rating值大的必须比rating值小的分 配的糖果多(rating相同的没必要分配一样多的糖果). 请计算最少需要多少颗糖果,才能完成上述分配. 输入格式: 多组数据,每组数据第一行是一个正整数n. 接下

编程挑战高校俱乐部分配糖果答案

题目详情 有n个小朋友站成一排(编号从0到n-1),每个小朋友有一个rating值,存放在ratings数组中.老师需要给他们分配糖果,每个小朋友至少需要一颗糖果,对于任意相邻的两个小朋友i和i+1,rating值大的必须比rating值小的分配的糖果多(rating相同的没必要分配一样多的糖果). 请计算最少需要多少颗糖果,才能完成上述分配. 输入格式: 多组数据,每组数据第一行是一个正整数n. 接下来n行,每行有1个正整数,表示每个小朋友的rating值.所有整数都不超过100000. 输出

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

[leet code 135]candy

1 题目 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 can

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

最少糖果问题.一排小孩,每个孩子有一个优先级,每个孩子至少要发给一个糖果,优先级高的比周围的孩子的糖果要多. 需要注意的是,优先级一样的没有要求说一样多糖果! 先初始化,每人一糖. 为了保证优先级大的比相邻的且优先级小的要糖果多.所以我们分两次处理,一次处理比左边的多,一次处理兼顾左边的多的情况下比右边的多.(如果优先级满足多的前提) 那么第一次从左到右,if (ratings[i] > ratings[i-1]) 那么cd[i] = cd[i-1]+1;    cd指糖果数组 从右边到左边需要

575. 分配糖果 Distribute Candies

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister.