[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。正向遍历一次,还是比较好想到的,那为什么要遍历两次,若整个数列是递减数列,则若只正向遍历一遍,所有得到的糖果都是1,这显然不符合题意,如4,3,2,1 ,其实只要开始时降序的就不行。那么方向遍历过程中,是不是只要前者比后者大就加1了?不是,我们举个反例“4,1,2,1”,正向遍历是,这四个小孩的得到的糖果数是:1,1,2,1,那么反向遍历时,若只要前者比后者大就加1会得到: 2,1,3,1,这显然不符合最少糖果的要求,最少应为2,1,2,1,这是因为正向遍历中,数组A[2]对应的元素值比两边都大,已经满足了其所得糖果比两边大的条件。总结一下:正向遍历,无法满足数组首元素为降序的情况;反向遍历,若只考虑前者比后者大,不满足若某元素已经取得局部最大的情况。所以此时应该加一定的限制条件,如代码所示:

 1 class Solution {
 2 public:
 3     int candy(vector<int> &ratings)
 4     {
 5         int len=ratings.size();
 6         int res=0;
 7         vector<int> cans(len,1);
 8         if(len<1) return 0;
 9
10         for(int i=0;i<len-1;++i)
11         {
12             if(ratings[i]<ratings[i+1])
13                 cans[i+1]=cans[i]+1;
14         }
15         for(int i=len-1;i>0;i--)
16         {
17             if(ratings[i]<ratings[i-1]&&cans[i-1]<=cans[i])  //限定
18                 cans[i-1]=cans[i]+1;
19         }
20
21         for(int i=0;i<len;++i)
22             res+=cans[i];
23
24         return res;
25     }
26 };

还有一种空间复杂度为O(1)的解法,见GeekFans的博客,不过个人感觉思想类似,有兴趣可以看看。

时间: 2024-08-04 08:33:51

[Leetcode] candy 糖果的相关文章

[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

[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 [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

[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

币圈进化,IFO、IHO玩法更高级,李笑来Candy“糖果”野望未来

如果说"链圈一日,人间一月"是区块链创业者们的感叹,那么"币圈一日,人间一年"则是玩币一族对数字货币的惊叹. 不管是区块链创业者还是数字货币玩家,如果不紧跟节奏,很快就会成为落后分子. 何玺今天要聊的,是当前区块链领域发展十分迅猛.火热的李笑来"糖果(分叉币)". 一.糖果(分叉币)的由来 我们知道,总量有限的比特币背后其实是一套基于算法的技术.随着比特币的不断发展,当前的可挖的比特币数量已经越来越少(目前,比特币全球仅剩420万枚可挖).与比特

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