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 candies than their neighbors.

What is the minimum candies you must give?

【题意】

有N个孩子排成一排,每个孩子有一个分值,按照下面的两条规则给孩子分饼干

1. 每个孩子至少有一块饼干

2. 分值高的孩子比他相邻孩子拿的饼干要多

问,最少给多少块饼干就可以了?

【思路】

不多给,也不少给,分值高的比相邻的多拿一块即可

首先给每个孩子发一块饼干

然后,每个孩子跟他左边的孩子比,从左到右检查每个孩子,如果这个孩子的分值比他左边孩子的要高,我们再给他些饼干,让他比他左边的孩子多一块饼干。

然后,每个孩子跟他右边的孩子比,从右到左检查每个孩子,如果这个孩子的分值比他右边孩子的要高,而他的饼干数没右边孩子的多,我们要给他些饼干,让他比他右边的孩子多一块饼干。

【代码】

class Solution {
public:
    int candy(vector<int> &ratings) {

        int sum=0;
        int size=ratings.size();
        vector<int> candy(size, 1);

        //从左到右检查每个孩子的左邻居
        for(int i=1; i<size; i++){
            if(ratings[i]>ratings[i-1])
                candy[i]=candy[i-1]+1;
        }
        //从右到左检查每个孩子的右邻居
        for(int i=size-2; i>=0; i--){
            if(ratings[i]>ratings[i+1] && candy[i]<=candy[i+1])
                candy[i]=candy[i+1]+1;
        }

        //计算饼干数
        for(int i=0; i<size; i++)
            sum+=candy[i];

        return sum;
    }
};

LeetCode: Candy [135]

时间: 2024-08-25 12:04:28

LeetCode: Candy [135]的相关文章

[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

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, 解题报告

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

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] 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 can

[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 Crush 糖果粉碎

This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D integer array board representing the grid of candy, different positive integers board[i][j] represent different types of candies. A value of board[i][j] = 0