/**
* 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. oracle主键和索引

    主键:能够唯一标识一条记录的字段为主键(亦或主码),不能重复的,不允许为空.作用:用来保证数据完整性个数:主键只能有一个 索引:作用:是提高查询排序的速度个数:一个表可以有多个索引 常用索引类型:No ...

  2. [POJ3630]Phone List (Tire)

    题意 trie字典树模板 LOJ有中文翻译https://loj.ac/problem/10049 思路 TIRE 代码 之前在LOJ上做过 直接交了 #include<cstdio> # ...

  3. AGC001F - Wide Swap

    Description 给你一个长度为$n$的排列,每次可以交换$|i-j|\geq K$并且$|a_i-a_j|=1$的数对,问你经过若干次变换后最小字典序的排列是啥 Solution 对$a$做一 ...

  4. 学习--->更新集合/内存/深浅拷贝

     一.计算机基础 1..软件(应用程序) 2.解释器/编译器 - 解释型语言:将代码每一行传递给计算机一行,常用编程语言python,PHP,Ruby. - 编译型语言:将代码完全执行完后会形成一个文 ...

  5. Spark内部执行机制

    Spark内部执行机制 1.1 内部执行流程 如下图1为分布式集群上spark应用程序的一般执行框架.主要由sparkcontext(spark上下文).cluster manager(资源管理器)和 ...

  6. 180815 Python自学成才001

    1.为什么学习Python? Python:脚本语言,易入门,可移植. Python适用范围:web开发.自动化测试工具编写. 适用岗位:运维开发(运维).自动化测试(软件测试).Python开发(软 ...

  7. RDD算子

    RDD算子 #常用Transformation(即转换,延迟加载) #通过并行化scala集合创建RDD val rdd1 = sc.parallelize(Array(1,2,3,4,5,6,7,8 ...

  8. python 错误记录

    class Func: d = dict() def __setitem__(self, key, value): # xxx object does not support item assignm ...

  9. verilog中timescale

    1. `timescale `timescale是Verilog中的预编译指令,指定位于它后边的module的时间单位和时间精度,直到遇到新的`timescale指令或者`resetall指令.它的语 ...

  10. thinkphp 找数据库某个字段为空的数据,PHP 数据调取 空数据

    $arr['dingwei'] = array('EXP','is null');