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. 7款HTML5的精美应用教程让你立即爱上HTML5

    1,HTML5/jQuery雷达动画图表图表配置十分简单 分享一款很特别的HTML5图表,它是利用HTML5和jQuery的雷达动画图表,图表数据在初始化的时候带有一定动画. 在线演示 源码下载 2, ...

  2. 大饱眼福 7款类型各异的CSS3实用菜单

    1.清新小图标的HTML5/CSS3侧边栏菜单 前我们分享过几款CSS3侧边栏菜单,像CSS3侧边栏菜单 带可爱的小图标菜单和CSS3侧边栏手风琴菜单,都非常不错.今天我们要分享的这款CSS3侧边栏菜 ...

  3. ADO.NET笔记——利用Command对象的ExecuteScalar()方法返回一个数据值

    相关知识: 有些SQL操作,例如SUM,只会从数据库返回一个数据值,而不是多行数据 尽管也可以使用ExecuteReader()返回一个DataReader对象,代表该数据值,但是使用Command对 ...

  4. JQuery在页面中添加和除移DOM

    1.before():将新节点添加到前面 2.after():将节点添加到低部 3.prepend():把节点变成第一个节点 4.append():把新节点添加到末端,与appendTo()效果相同, ...

  5. c#枚举自定义,用于数据绑定。

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Enum)] public ...

  6. WPF 绑定三(绑定List中指定的字符串)

    xaml: <Window x:Class="WpfApplication1.Window3" xmlns="http://schemas.microsoft.co ...

  7. Oracle内存组件理论篇一

    目标 1.SGA结构 2.PGA结构 1.SGA Shared pool 1).共享池是对SQL.PL/SQL程序进行语法分析.编译.执行的内存区域. 在执行SELECT * FROM emp语句时, ...

  8. NodeJS - Express 3.0下ejs模板使用 partial展现 片段视图

    如果你也在看Node.js开发指南,如果你也在一步一步实现 microBlog 项目!也许你会遇到本文提到的问题,如果你用的是Express 3.0 本书实例背景是 Express 2.0 而如今升级 ...

  9. 图片轮播插件-carouFredSel

    carouFredSel图片轮播插件基于Jquery,比较常规的轮播插件,支持滚轮及键盘左右按键,加入其它插件可实现更加复杂的特效. 主页地址:http://caroufredsel.dev7stud ...

  10. Android SDK Android NDK 官方下载地址

    Android NDK r6b Windows http://dl.google.com/android/ndk/android-ndk-r6b-windows.zip Mac OS X(intel) ...