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. Python操作sqlite数据库小节

    学习了Python操作sqlite数据库,做一个小结,以备后用. import sqlite3import os# 进行数据库操作时,主要是参数如何传输try:# 链接数据库conn=sqlite3. ...

  2. PHP生成月历代码

    <?php /*   Function Written by Nelson Neoh @3/2004.   For those who wants to utilize this code, p ...

  3. 简单便捷的纯PHP网盘程序 Veno File Manager 2.6.3(VFM2)

    体验过很多国外网盘程序,例如:Owncloud.Bedrive.YetiShare.XFilesharing.uCloud.Cloudshare 等等,诸如此类,VFM2与这些臃肿的商用或非商用来的程 ...

  4. 在Android上编译OSG[3.0.2 ] (转)

    在Android上编译OSG[3.0.2 ] 分类:Android   This file contents can be applied for version OpenSceneGraph(OSG ...

  5. Flume NetCat Demo

    准备工作: 1.apache官网下载flume 2.解压flume 3.修改flume-env.sh,配置JAVA_HOME NetCat采集Demo: 1.在conf中创建netcat-logger ...

  6. 正确理解hadoop 2.x 的环形缓冲区: (一) MR环形缓冲区的结构

    转载:http://blog.csdn.net/HADOOP_83425744/article/details/49560583 http://bigdatadecode.club/MapReduce ...

  7. VS2010 + C#4.0使用 async + await

    方法一: 安装官方出的Microsoft.Bcl.Async包 最新发布日期为 2014/4/12,版本1.0.168 (不支持VS2010) 1.解决方案-右键-管理解决方案的NuGet程序包 2. ...

  8. android listView 滑动载入数据 该数据是服务端获取的

    package com.sunway.works.applycash; import java.util.ArrayList; import java.util.Calendar; import ja ...

  9. HTML5 Canvas 绘制英国国旗

    代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...

  10. sublime添加sass编译

    首先安装Ruby环境sass是基于ruby的产物,因此在安装sass前需要先安装ruby,如果用命令方式编译Sass也是必须安装ruby的.命令行编译sass见!下载Ruby windows 安装包: ...