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?

Runtime: 392 ms

这道题跟Leetcode 一道contain water的题很像,思想很简单,可以把每个小朋友单独考虑,看下每个小朋友左边有紧邻的小朋友rate降序的个数,再看下该小朋友右边紧邻的小朋友降序的个数,然后就能确定本小朋友的最小“高度”(两降序个数中较大值),即为小朋友应得的糖果。

方法:从前往后,从后往前扫描两遍,并用lowleft[],lowright[]分别记录每个小朋友两边的降序个数即可~

public class Solution {

public int candy(int[] ratings) {

if(ratings.length==0) return 0;

if(ratings.length==1) return 1;

int[] lowleft=new int[ratings.length];

int[] lowleft=new int[ratings.length];

int ret=0;

lowleft[0]=0;

for(int i=1;i<ratings.length;i++){

if(ratings[i-1]<ratings[i]) lowleft[i]=lowleft[i-1]+1;

//else if(ratings[i-1]==ratings[i]) lowleft[i]=lowleft[i-1];

else lowleft[i]=0;

}

lowright[ratings.length-1]=0;

for(int j=ratings.length-2;j>=0;j--){

if(ratings[j+1]<ratings[j]) lowright[j]=lowright[j+1]+1;

//else if(ratings[j+1]==ratings[j]) lowright[j]=lowleft[j+1];

else lowright[j]=0;

}

for(int i=0;i<ratings.length;i++){

ret=ret+1+Math.max(lowright[i],lowleft[i]);

}

return ret;

}

}

[LeetCode][Java]Candy@LeetCode的更多相关文章

  1. [LeetCode][Java]Triangle@LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  2. 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 ...

  3. 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. ...

  4. 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 ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. 2o_TwoTips

    ∮Linux 使用的两个小技巧 开启启动脚本 和 环境变量问题 §1.开机启动脚本 && 周期任务 环境 CentOS7:3.10.0-327.el7.x86_64 # head /e ...

  2. Java三大框架的配置

    1. 首先是spring,右键项目-myeclipse-capabilitise-install spring etc.类似的就好,生成applicationContext.xml和spring一些类 ...

  3. 转 Jmeter之Bean shell使用(二)

    上一篇Jmeter之Bean shell使用(一)简单介绍了下Jmeter中的Bean shell,本文是对上文的一个补充,主要总结下常用的几种场景和方法,相信这些基本可以涵盖大部分的需求.本节内容如 ...

  4. python迭代器,生成器,装饰器,context模块

    迭代器iteration 是访问集合元素的一种方式,只能往前不能往后迭代器的特点:1,访问者不需要关注迭代器内部结构,只需通过next()不断取下一个内容2,访问不能回退3,循环较大数据集合时,省内存 ...

  5. SRM 146 DIV1 600

    Problem Statement      Masterbrain is a two player board game in which one player decides on a secre ...

  6. 15.6.2 Configuring the Merge Threshold for index pages[innodb]

    MERGE THRESHOLD 提供了可以合并相邻索引page的功能. 默认值是50 如果一个页中数据被删除或者更新减小,导致页中有空白部分,空白部分接近合并门槛的值,则会和相邻页合并, 但是两个pa ...

  7. 读Javascript高级程序设计第三版第六章面向对象设计--创建对象

    虽然Object构造函数或者对象字面量都可以用来创建单个对象,但是缺点非常明显:使用同一接口创建很多对象,会产生大量重复代码. 工厂模式  1 function CreatePerson(name,a ...

  8. .NET Core Web 应用部署到 Docker 中运行

    环境介绍 : 虚拟机:VirtualBox 5.1.6 系 统:Ubuntu 16.04.1 LTS 系统准备完成后可以使用 sudo apt-get udpate 和 sudo apt-get up ...

  9. CSS属性(根据继承性分为两类)

    一.可继承属性 1>所有标签可继承: visibility:行高 cursor: 2>内联标签可继承: line-height:行高 color:文字颜色 font-family:文字字体 ...

  10. ubuntu 16 mysql安装包安装 (推荐在线安装)

    /etc/init.d/中的文件命名为mysql cp好各种文件后 ./mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/my ...