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?

Solution:

最开始想到的是 从左往右循环,如果遇到 左边小于右边的, 右边的+1。 遇到左边大于右边的, 回退,直到右边大于左边,给每一个元素+1, 这样时间复杂度是O(n^2)。 得降:

接着就想到用stack,循环从左边开始,如果发现左边比右边大 则入stack,直到左边比右边小 ,然后出stack,给每个出stack的数加上其在stack里面的位置,即深度。

如果当前点比它前面的点大呢? candy[i] = candy[i - 1] + 1; 否则, candy[i] = 1;

这里新建了一个数组,rating, 它扩展了原数组,末尾加了一个-1, 用于对最后一个元素进行判断。

对于栈底元素,即临界元素,其值应该等于左边得到的值 和通过栈的到的值中间最大的那一个。

还要在循环外, 对stack进行一次操作。

对最后一个点 还得讨论,

1)如果最后一个点 比 前一个小 则不变
2)比前一个大 则为D(n -1) + 1
 
464 ms过大测试~~时间复杂度 O(n)。
 public class Solution {
public int candy(int[] ratings) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int l = ratings.length;
int[] rating = new int[l + 1];
for(int i = 0; i < l; i ++){
rating[i] = ratings[i];
}
rating[l] = -1;
int sum = 0;
int d = 0;
int[] candy = new int[l];
Stack<Integer> st = new Stack<Integer>();
for(int i = 0; i < l; i ++){
if(i > 0 && rating[i] > rating[i - 1]){
candy[i] = candy[i - 1] + 1;
}else{
candy[i] = 1;
}
if(rating[i] > rating[i+1]){
st.push(i);
}else{
d = st.size();
if(d > 0){
for(int ii = 0; ii < d - 1; ii ++){
int cur = st.pop();
candy[cur] += ii+1;
}
int cur = st.pop();
candy[cur] = ((d + 1) > candy[cur] ? (d + 1) : candy[cur]);// d+1 原因: 最小的那个元素没有入栈,栈的深度少了1.
}
} }
d = st.size();
for(int ii = 0; ii < d - 1; ii ++){
int cur = st.pop();
candy[cur] += ii;
}
int cur = st.pop();
candy[cur] = (d > candy[cur] ? d : candy[cur]);
for(int i = 0; i < candy.length; i ++){
sum += candy[i];
}
return sum;
}
}

其实,这一题可以想象成一个波, 它有上升和下降。 第一遍,考虑上升的所以情况; 第二遍,考虑下降的所以情况。 然后对于波峰,用两边的max 值当成它的值即可。

这样 思路变得更加清晰。

 public class Solution {
public int candy(int[] ratings) {
// Note: The Solution object is instantiated only once and is reused
//by each test case.
int rLen = ratings.length;
if (rLen == 0) return 0;
int min = rLen; int give = 0;
int[] gives = new int[rLen];
for (int i = 1; i < rLen; i++) {
if (ratings[i] > ratings[i - 1]) give++;
else give = 0;
gives[i] = give;
}
give = 0;
for (int i = rLen - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) give++;
else give = 0;
min += Math.max(give, gives[i]);
}
min += gives[rLen - 1];
return min;
}
}

find out all local min rating, 
for each local min rating, start with 1 candy, and expand on both directions
until hit by local max.
return total candies.

O(n)

第二遍: 波的方法, 左边走一次右边走一次。

 public class Solution {
public int candy(int[] ratings) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(ratings == null || ratings.length == 0) return 0;
int len = ratings.length;
int[] candy = new int[len];
int sum = 0;
for(int i = 0; i < len; i ++)
candy[i] = 1;
for(int i = 1; i < len; i ++){
if(ratings[i - 1] < ratings[i]) candy[i] = candy[i - 1] + 1;
}
for(int i = len - 1; i > 0; i --){
if(ratings[i] < ratings[i - 1]) candy[i - 1] = Math.max(candy[i - 1], candy[i] + 1);
}
for(int i = 0; i < len; i ++)
sum += candy[i];
return sum;
}
}

Candy的更多相关文章

  1. [LeetCode] Candy 分糖果问题

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

  2. Leetcode Candy

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

  3. LeetCode 135 Candy(贪心算法)

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

  4. [LeetCode][Java]Candy@LeetCode

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

  5. 【leetcode】Candy(hard) 自己做出来了 但别人的更好

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

  6. 【leetcode】Candy

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

  7. Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s

    C. Inna and Candy Boxes   Inna loves sweets very much. She has n closed present boxes lines up in a ...

  8. [LintCode] Candy 分糖果问题

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

  9. POJ - 1666 Candy Sharing Game

    这道题只要英语单词都认得,阅读没有问题,就做得出来. POJ - 1666 Candy Sharing Game Time Limit: 1000MS Memory Limit: 10000KB 64 ...

  10. Candy Store

    Candy Store Time Limit: 30000ms, Special Time Limit:75000ms, Memory Limit:65536KB Total submit users ...

随机推荐

  1. window store app 附件读取

    public static async Task<bool> DisplayApplicationPicker(string folderName, string fileName) { ...

  2. linux 终端显示 -bash-4.1

    解决方法: cp /etc/skel/.bashrc /root/ cp /etc/skel/.bash_profile  /root/ 重新登陆就OK了

  3. 11g RAC R2 体系结构---进程,日志

    进程结构:Overview of Oracle Clusterware Platform-Specific Software Components When Oracle Clusterware is ...

  4. C# 实例化接口对象

    在head first 设计模式中 用到了很多很多接口对象 首先澄清一个问题,就是接口不仅可以声明对象,而且可以把对象实例化,还可以当做参数被传入. 一.接口回调 这就是继承中的向上转型.父类 FL= ...

  5. LeapMotion(1):环境配置、简单测试、理解对象

    关注Leap Motion很长时间了,很早就想入手.可是,一方面,一直忙着其它的比赛,没时间顾及:二是缺钱,钱都垫在比赛上了. 好不容易,11月18日,下定决心买进了,这么长时间,也就是再给贵阳职业学 ...

  6. 管道和FIFO

    pipe 子进程从终端读取一个文件名, 通过管道将文件名传递给父进程 父进程收到文件名后, 读取文件内容并通过管道传递给子进程 子进程接收到文件内容并输出到终端 #include <stdio. ...

  7. 第六周 E题 期望.....

    Description Given a dice with n sides, you have to find the expected number of times you have to thr ...

  8. Vim配置IDE开发环境

    我的vim IDE界面: 1.安装Vim和Vim基本插件首先安装好Vim和Vim的基本插件.这些使用apt-get安装即可:lingd@ubuntu:~/arm$sudo apt-get instal ...

  9. iTween基础之Scale(缩放大小)

    一.基础介绍:二.基础属性 原文地址:http://blog.csdn.net/dingkun520wy/article/details/50684392 一.基础介绍 ScaleTo:改变游戏对象的 ...

  10. 20145120 《Java程序设计》第4周学习总结

    20145120 <Java程序设计>第4周学习总结 教材学习内容总结 -定义子类,加"extends+父类名"以继承父类. -子类只能继承一个父类 -编辑器会检查等号 ...