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. 想给自己的实景三维模型做个案例集?Wish3D Earth再合适不过了

    很多朋友向用户展示实景三维模型的时候经常面临这样的问题:

  2. mysql跨服务器查询

    MySQL FEDERATED引擎使用示例, 类似Oracle DBLINK 摘要: 本地MySQL数据库要访问远程MySQL数据库的表中的数据, 必须通过FEDERATED存储引擎来实现. 有点类似 ...

  3. centos DHCP

    yum install dhcp cat /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example > /etc/dhcp/dhcpd.conf vim /etc ...

  4. Android4.0(Phone)拨号启动过程分析(一)

    因为工作的须要.须要改动原生的Phone程序,如今就好好看下来电与拨号是怎样处理的:无论是拨号还是来电,调用的都是Phone程序,因为非常多类都涉及到framework层,比較复杂:先从简单的拨号分析 ...

  5. Linux的经常使用命令(2) - 关机

    关机命令 shutdown‑h now 马上进行关机 shutdown‑r now 如今又一次启动计算机 -t sec : -t后面加秒数,即"过几秒后关机" -k      : ...

  6. 【Python】从文件中读取数据

    从文件中读取数据 1.1 读取整个文件 要读取文件,需要一个包含几行文本的文件(文件PI_DESC.txt与file_reader.py在同一目录下) PI_DESC.txt 3.1415926535 ...

  7. swiper-demo1

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. python中executemany的使用

    conn = MySQLdb.connect(host = “localhost”, user = “root”, passwd = “password”, db = “myDB”, charset= ...

  9. 在windows下安装apidocjs

    1. 下载Node.js官方Windows版程序:   https://nodejs.org/download/   从0.6.1开始,Node.js在Windows平台上提供了两种安装方式,一是.M ...

  10. netty 对 http 的实现

    netty的http协议栈无论是性能还是可靠性,都表现优异,非常适合在非web容器场景 下应用,相比于tomcat.jetty等web容器,它更轻量.小巧.灵活性和定制性也好: 总结:只要实现了htt ...