/**
* Source : https://oj.leetcode.com/problems/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?
*
*/
public class Candy { /**
* 给站成一排的孩子分糖果,
* 每个孩子至少一个
* 每个孩子的权重不一样,权重高的孩子比相邻的孩子要多
* 求最少需要多少个糖果
*
* 从左到右遍历,寻找递增序列,该位置对应的孩子分的糖果数也递增,每个递增序列从1开始,这样保证了rating高的孩子比左边的孩子多
* 然后从右向左遍历,确保当前位置孩子的糖果数多于右边孩子的
* 复杂度为O(n)
*
* @param ratings
* @return
*/
public int candy (int[] ratings) {
if (ratings.length <= 0) {
return 0;
}
int[] candy = new int[ratings.length];
candy[0] = 1;
for (int i = 1; i < ratings.length; i++) {
if (ratings[i] > ratings[i-1]) {
candy[i] = candy[i-1] + 1;
} else {
candy[i] = 1;
}
}
int sum = candy[ratings.length-1];
for (int i = ratings.length-2; i > -1; i--) {
if (ratings[i] > ratings[i+1] && candy[i] <= candy[i+1]) {
candy[i] = candy[i+1] + 1;
}
sum += candy[i];
}
return sum;
} public static void main(String[] args) {
Candy candy = new Candy();
int[] ratings = new int[]{5, 6, 7, 4, 1, 2, 3, 2, 1, 7};
System.out.println(candy.candy(ratings) + " == 19");
}
}

leetcode — candy的更多相关文章

  1. [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现

    [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...

  2. [LeetCode] Candy 分糖果问题

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

  3. [LeetCode] Candy Crush 糖果消消乐

    This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...

  4. [leetcode]Candy @ Python

    原题地址:https://oj.leetcode.com/problems/candy/ 题意: There are N children standing in a line. Each child ...

  5. Leetcode Candy

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

  6. LeetCode: Candy 解题报告

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

  7. [Leetcode] candy 糖果

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

  8. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  9. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

随机推荐

  1. TimesTen数据库表中显示中文乱码的真正原因

    上一篇博客TimesTen中文乱码问题(其实是cmd.exe中文乱码)的内容可能不对,也许只是个巧合?不得而知了.因为我今天重装系统了,把win10换成了win7(64bit).又安装了timeste ...

  2. 全民https时代,Let's Encrypt免费SSL证书的申请及使用(Tomcat版)

    近几年,在浏览器厂商的强力推动下,HTTPS的使用率大增.据统计,Firefox加载的网页中启用HTTPS的占比为67%,谷歌搜索结果中HTTPS站点占比已达50%,HTTPS网站已获得浏览器和搜索引 ...

  3. css3_transition: 体验好的过渡效果。附 好看的按钮

    利用css的transition属性详解,上图就是利用transition效果做的一个按钮. transition属性://举例子:transition:all 1s ease;transition: ...

  4. jquery项目中一些常用方法

    1.获取url中的参数 function getUrlParam(name) {    var reg = new RegExp("(^|&)" + name + &quo ...

  5. 找一个数组的最大和的连续子数组(时间复杂度 O(n))(二)

    要求: 要求数组从文件读取. 如果输入的数组很大,  并且有很多大的数字,  就会产生比较大的结果 (考虑一下数的溢出), 请保证你的程序能正常输出. 另外, 如果输入文件的参数有错误, 这个程序应该 ...

  6. sqlmap Windows 安装教程

    第一步:下载 python :https://www.python.org/downloads/    (这里有python各种版本,但是一般建议安装3和2.7) sqlmap:https://git ...

  7. python scrapy框架爬取豆瓣

    刚刚学了一下,还不是很明白.随手记录. 在piplines.py文件中 将爬到的数据 放到json中 class DoubanmoviePipelin2json(object):#打开文件 open_ ...

  8. Android简单计时器

    本文利用ContextMenu(上下文菜单),Chronometer实现简单计数器. Main.xml: <?xml version="1.0" encoding=" ...

  9. Linux下CenOS系统 安装Redis

    1.redis下载 进入root目录:cd /root(目录可自定义)   wget http://download.redis.io/releases/redis-3.2.10.tar.gz 红色部 ...

  10. nova boot from volume代码分析

    首先要创建一个bootable volume curl -i http://16.158.166.197:8776/v1/c24c59846a7f44538d958e7548cc74a3/volume ...