算法描述:

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?

Example 1:

Input: [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.

Example 2:

Input: [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.The third child gets 1 candy because it satisfies the above two conditions.

解题思路:先从左往右扫,遇到当前小孩rating值大于前一个小孩rating值,则给该小孩分的糖置为前一个小孩糖果数加一。然后从右往左扫,遇到当前小孩rating值小于后面小孩的rating值,该小孩的糖果数为当前分配的糖果数和后面小孩分配的糖果数加一两者之中的最大值。

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

转载于:https://www.cnblogs.com/nobodywang/p/10392534.html

LeetCode-135-Candy相关推荐

  1. 【重点】LeetCode 135. Candy

    LeetCode 135. Candy 博客转载自:http://www.cnblogs.com/grandyang/p/4575026.html 这个题一看就很难...但答案真是清新脱俗啊... S ...

  2. [LeetCode]135.Candy

    [题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  3. leetcode 135. Candy | 135. 分发糖果(原创图文详解,Java)

    题目 https://leetcode.com/problems/candy/ 题解 思路 首先,根据题意,这是一个分糖果问题.本题需要满足两个条件: 每个孩子至少有一个糖果 rating 值较大的孩 ...

  4. LeetCode 135. Candy

    找出每个child分别相对于左边和右边的最长递增序列长度,取两者最大值就是他最后应该获得的糖数 class Solution {public int candy(int[] ratings) {int ...

  5. Java描述 LeetCode,135. Candy 分发糖果

    大家好,我是河海哥,专注于后端,如果可以的话,想做一名code designer而不是普通的coder,一起见证河海哥的成长,您的评论与赞是我的最大动力,如有错误还请不吝赐教,万分感谢.一起支持原创吧 ...

  6. LeetCode之Candy

    [题目] There areNchildren standing in a line. Each child is assigned a rating value. You are giving ca ...

  7. LeetCode 135. 分发糖果(DP)

    1. 题目 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果. 相邻的 ...

  8. 2022-4-10 Leetcode 135.分发糖果

    官方题解 class Solution {public:int candy(vector<int>& ratings) {int n = ratings.size();vector ...

  9. 170. Leetcode 135. 分发糖果 (贪心算法-两个维度权衡题目)

    这道题目一定是要确定一边之后,再确定另一边,例如比较每一个孩子的左边,然后再比较右 边,如果两边一起考虑一定会顾此失彼. 先确定右边评分大于左边的情况(也就是从前向后遍历) 此时局部最优:只要右边评分 ...

  10. leetcode 135分发糖果

    这是一道hard题,不好想,但最后还是想出来了,私以为还是根据一些思想方法自己想出来做法印象比较深刻,其次看人家的做法思想自己写代码,其次看代码理解默写,其次直接抄代码: 首先,给每个孩子都发一个糖果 ...

最新文章

  1. 我和我的Android
  2. ​他被称为印度“ IT 大王”,富可敌国,却精打细算如守财奴
  3. python tkinter chk
  4. 2.word转换为pdf
  5. IDEA中识别Git上新加的分支
  6. hdu 2028 Lowest Common Multiple Plus(最小公倍数)
  7. linux 命令学习
  8. Java代码操作数据库(上)——JDBC(JDBC、JUnit、PreparedStatement) 练习
  9. discuz内置代码
  10. 北美票房:《触不可及》惊喜夺冠
  11. #ArcGis中如何对属性表中的字段进行顺序赋值??
  12. 重生之我是赏金猎人-SRC漏洞挖掘(一)-某SRC测试系统无脑Getshell
  13. 如何建立一个网站?规划、设计、目的、原则、宣传(一)
  14. html页面美化代码时间,CSS+JS美化过漂亮的日历控件
  15. 2010年国家规划布局内重点软件企业认定名单
  16. Centos8 中如何使用journalctl分析日志
  17. Hibernate-基础学习
  18. 瑞盟MS8416光纤同轴解码芯片替代CS8416
  19. 9套Android实战经典项目资料分享给大家
  20. 任正非:华为十大管理要点

热门文章

  1. C#内置函数 RunSql的使用
  2. git review devops过程
  3. IOS入门-TargetAction
  4. 5.8fork父子进程
  5. [转贴]2006十大经典语句
  6. php 的opcode缓存apc以及其安装
  7. ASP.NET实现数据图表
  8. Ubuntu查看系统位数及版本
  9. WMI技术介绍和应用——查询桌面信息
  10. 一种不会导致资源泄露的“终止”线程的方法