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?

解题思路:

双向爬坡问题,从前往后、从后往前各扫一遍,JAVA实现如下:

	public int candy(int[] ratings) {
int sum = 0;
int[] candies = new int[ratings.length];
for (int i = 0; i < ratings.length - 1; i++)
if (ratings[i] < ratings[i + 1])
candies[i + 1] = candies[i] + 1;
for (int i = ratings.length - 2; i >= 0; i--)
if (ratings[i + 1] < ratings[i])
candies[i] = Math.max(candies[i], candies[i + 1] + 1);
for (int i = 0; i < candies.length; i++)
sum += 1 + candies[i];
return sum;
}

Java for LeetCode 135 Candy的更多相关文章

  1. LeetCode 135 Candy(贪心算法)

    135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...

  2. leetcode 135. Candy ----- java

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  3. (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重

    原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...

  4. Java实现 LeetCode 135 分发糖果

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

  5. Leetcode#135 Candy

    原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...

  6. [leetcode] 135. Candy (hard)

    原题 前后两遍遍历 class Solution { public: int candy(vector<int> &ratings) { vector<int> res ...

  7. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  9. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

随机推荐

  1. Aspose.Total for .NET 2015 - Unlimited License z

    How to license Aspose.Total for .NET products Add "License.cs" [C#] OR "License.vb&qu ...

  2. hduoj1285确定比赛名次

     确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  3. CentOS下iptables 配置详解

    如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 我们来配置一个filter表的防火墙. (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables - ...

  4. Json格式化工具 JsonViewer下载

    免安装版,分享链接永久有效~! 云盘下载地址: http://cloud.suning.com/cloud-web/share/link.htm?sk=401f784782751055ddc21cdb ...

  5. Node.js自动化技术实现(Java)

    Node.js自动化测试框架(NodeTestFramework):

  6. 25. Spring Boot使用自定义的properties【从零开始学Spring Boot】

    转:http://blog.csdn.net/linxingliang/article/details/52069515 spring boot使用application.properties默认了很 ...

  7. 微信小程序(应用号)开发新闻客户端的实战课程

    摘要: 本实例将演示从零开发一个微信应用号的过程,页面轮播与跳转传值,实现单元格自定义布局,全部源码可通过github下载. 下载最新版的微信小程序开发工具,目前是v0.9.092300 下载地址:h ...

  8. 解决:cannot execute binary file

    linux系统下遇到cannot execute binary file的问题,一般由以下情况造成: 非root用户或者无执行权限 编译环境不同(程序由其他操作环境复制过来) 对于第一种情况,采用增加 ...

  9. 【转载】ASP.Net WebForm温故知新学习笔记:一、aspx与服务器控件探秘

    开篇:毫无疑问,ASP.Net WebForm是微软推出的一个跨时代的Web开发模式,它将WinForm开发模式的快捷便利的优点移植到了Web开发上,我们只要学会三步:拖控件→设属性→绑事件,便可以行 ...

  10. Hibernate学习一----------Hibernate初实现

    © 版权声明:本文为博主原创文章,转载请注明出处 ORM(Object/Relationship Mapping):对象/关系映射 - 利用面向对象思想编写的数据库应用程序最终都是把对象信息保存在关系 ...