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?

从后向前判断:

1、找出从小到大排列的子序列,大小为n,分配的糖果应该是1、2、3....n

2、如果 n == 1 那么就应该是比之前子序列的开头要大,即 : 如果之前子序列中数字个数大于1,那么该位置分配2个糖果(应该之后一个位置的孩子得到了1个糖果),否则得到比后一个位置的孩子糖果多1 的糖果。

3、比较麻烦的是如果遇到相同rating的孩子:相同rating 的孩子可以得到不一样的糖果。

  因此每次结束的时候需要判断当前子序列结尾的数字是否与下一个子序列开始的位置的数字相同,如果不相同,需要当前子序列结尾得到的数目大于在一个子序列开始的数目;如果一样,那么可以按照之前的顺序分配,即1、2、3、4...n

public class Solution {
public int candy(int[] ratings) { int len = ratings.length; int result = 0; int pos = len-1;
int flag = 0,target = 0;
while( pos >= 0){ int size = 1;
while( pos >= 1 && ratings[pos-1] < ratings[pos] ){
size++;
pos--;
target = ratings[pos];
} if( size > 1){
result+=(1+size)*size/2;
if(size <= flag ){
if( pos+size<len && ratings[pos+size] != ratings[pos-1+size])
result+=(flag-size+1);
}
flag = 1;
}
else {
if( pos<len-1 && ratings[pos] == ratings[pos+1]){
flag = 1;
result+=1;
}
else{
result += flag+1;
flag++;
}
} pos--; } return result; }
}

                

leetcode 135. Candy ----- java的更多相关文章

  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. Java for LeetCode 135 Candy

    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. Leetcode#135 Candy

    原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...

  5. [leetcode] 135. Candy (hard)

    原题 前后两遍遍历 class Solution { public: int candy(vector<int> &ratings) { vector<int> res ...

  6. Java实现 LeetCode 135 分发糖果

    135. 分发糖果 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果. ...

  7. [Leetcode 135]糖果分配 Candy

    [题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  8. 【LeetCode】135. Candy

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

  9. 135. Candy

    题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...

随机推荐

  1. IT公司100题-17-第一个只出现一次的字符

    问题描述: 在一个字符串中找到第一个只出现一次的字符.例如输入asdertrtdsaf,输出e.   分析: 最简单的方法是直接遍历,时间复杂度为O(n^2). 进一步思考: 字符串中的字符,只有25 ...

  2. “更高效率:标准化+简约风+移动化”--K2 BPM老客户交流会

    主题:工作流主数据标准化和移动工作流带来的企业沟通建设机会 嘉宾:李瑞延(盛大网络IT总监) 公司管理需要更好的工作流 -为决策提供依据 通过对各级业务公司各类流程数据的获取与分析,为管理决策提供必要 ...

  3. Android 自动朗读(TTS)

    在Android应用中,有时候需要朗读一些文本内容,今天介绍一下Android系统自带的朗读TextToSpeech(TTS).自动朗读支持可以对指定文本内容进行朗读,还可以把文本对应的音频录制成音频 ...

  4. SplashTop Remote + 4核android平板 试用

    局域网默认情况 最大100Mb 的网速下, 延迟在500ms+, 观看视频无影响, 但游戏无法进行! 另一种方案,利用多网卡来提升网络传输性能!

  5. hdu4417 划分树+二分

    //Accepted 14796 KB 453 ms //划分树 //把查询的次数m打成n,也是醉了一晚上!!! //二分l--r区间第k大的数和h比较 #include <cstdio> ...

  6. C# 使用命令行编译单个CS文件

    编译单个CS文件. 1.编译   File.cs   以产生   File.exe:       csc   File.cs     2.编译   File.cs   以产生   File.dll:  ...

  7. 全球著名的渗透测试Linux简介

    注:如发现链接无法打开,请尝试代理登录链接 1. Kali Linux Kali Linux是基于Debian的Linux发行版, 设计用于数字取证和渗透测试.由Offensive Security ...

  8. Ubuntu 14.10 下grep命令详解

    简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...

  9. 基于HTML5+CSS3的图片旋转、无限滚动、文字跳动特效

    本文分享几种基于HTML5+CSS3实现的一些动画特效:图片旋转.无限滚动.文字跳动;实现起来均比较容易,动手来试试! 一.图片旋转 效果图如下: 这个效果实现起来其实并不困难.代码清单如下: < ...

  10. Asp.Net中Ajax实现登陆判断

    Default.aspx: <head runat="server"> <title>无标题页</title> <script type= ...