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?
class Solution {
public:
    int candy(vector<int> &ratings) {
        std::vector<int> num(ratings.size(), 1);

        int inc = 2;
        for (int i = 1; i < ratings.size(); ++i) {
            if (ratings.at(i) > ratings.at(i - 1)) {
                num.at(i) = std::max(inc++, num.at(i));
            } else {
                inc = 2;
            }
        }
        inc = 2;
        for (int i = ratings.size() - 2; i >= 0; --i) {
            if (ratings.at(i) > ratings.at(i + 1)) {
                num.at(i) = std::max(inc++, num.at(i));
            } else {
                inc = 2;
            }
        }
        int res = std::accumulate(num.begin(), num.end(), 0);
        return res;
    }
};

step1. 首先将辅助数组每个元素初始化为1,因为每个元素都至少为1

step2. 从左向右扫描,如果某个数比其左相邻数大,则num数组对应位置的这个数从2开始递增赋值 (注意相等是不需要增加的),否则递增量初始为2

遍历完之后,可以满足:如果 原数组中某个数大于其左相邻数,那么对应辅助数组中也一定是大于其左相邻数

如果 ratings[i] >= ratings[i+1] 此时ratings[i+1]一定为1

step3. 从右向左遍历,类似step2

其实step2中 num.at(i)一定是等于inc++的,因为num.at(i)未改变之前都是初始为1的

为什么step3中一定要加上 inc比num.at(i)的比较呢? 因为此时 必须满足step2之后数组的大小条件

原数组为:1 --> 2 --> 5 --> 4 --> 3 --> 2 --> 1

step2之后 num数组为:1 --> 2 --> 3 --> 1 --> 1 --> 1 --> 1

step3从右向左遍历到idx=2时 : 1 --> 2 --> (3 or 5)--> 4 --> 3 --> 2 --> 1

idx=2位置处,必须满足它比左相邻数大,比右相邻数大,所以才有了max操作

时间: 2024-10-06 12:44:35

Leetcode:Candy 每个数都比相邻数大的相关文章

(hdu step 2.3.7)下沙的沙子有几粒?(简单DP:求有m个H,n和D,且任意索引上H的个数都要比D的个数多的方案数)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: 下沙的沙子有几粒? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 894 Accepted Submission(s): 505   Problem Descri

LeetCode || Candy

Candy Total Accepted: 12392 Total Submissions: 68386My Submissions 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

[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

LeetCode: Candy [135]

[题目] 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

CCF - 201409-1 - 相邻数对

问题描述 试题编号: 201409-1 试题名称: 相邻数对 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1. 输入格式 输入的第一行包含一个整数n,表示给定整数的个数. 第二行包含所给定的n个整数. 输出格式 输出一个整数,表示值正好相差1的数对的个数. 样例输入 610 2 6 3 7 8 样例输出 3 样例说明 值正好相差1的数对包括(2, 3), (6, 7), (7, 8). 评测用例规模与约定 1

[leetcode]Candy @ Python

原题地址:https://oj.leetcode.com/problems/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

[算法]数组排序之后相邻数的最大差值

题目: 给定一个整形数组arr,返回排序后的相邻两数的最大差值. 时间复杂度为O(N). 解答: 如果用排序法实现,其时间复杂度为O(NlogN),而如果利用桶排序的思想(不是桶排序),可以做到O(N),额外空间复杂度为O(N).遍历arr找到最大值max和最小值min.如果arr的长度为N,准备N+1个桶,把max单独放在第N+1个桶中,[min,max)范围上的数放在1~N号桶里,对于1~N号桶中的每一个桶来说,负责的区间为(max-min)/N.如果一个数为num,它应该分配进(num-m

[LeetCode]Candy, 解题报告

前言 回学校写论文差不多快二周的时间了,总结一句话,在学校真舒服.花了7天时间搞定了毕业论文初稿(之所以这么快肯定跟我这三年的工作量相关了),不过今天导师批复第二章还是需要修改.此外,最近迷上打羽毛球,确实很爽,运动量仅此于篮球,可以场地有限,哎,且打且珍惜吧. 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these chi

一个数如果恰好等于它的因子之和,这个数就称为&quot;完数&quot;。 例如,6的因子为1、2、3,而6=1+2+3,因此6是&quot;完数&quot;。 编程序找出N之内的所有完数,

题目描述 一个数如果恰好等于它的因子之和,这个数就称为"完数". 例如,6的因子为1.2.3,而6=1+2+3,因此6是"完数". 编程序找出N之内的所有完数,并按下面格式输出其因子: 输入 N 输出 ? its factors are ? ? ? 样例输入 1000 样例输出 6 its factors are 1 2 3 28 its factors are 1 2 4 7 14 496 its factors are 1 2 4 8 16 31 62 124