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?
Solution:
最开始想到的是 从左往右循环,如果遇到 左边小于右边的, 右边的+1。 遇到左边大于右边的, 回退,直到右边大于左边,给每一个元素+1, 这样时间复杂度是O(n^2)。 得降:
接着就想到用stack,循环从左边开始,如果发现左边比右边大 则入stack,直到左边比右边小 ,然后出stack,给每个出stack的数加上其在stack里面的位置,即深度。
如果当前点比它前面的点大呢? candy[i] = candy[i - 1] + 1; 否则, candy[i] = 1;
这里新建了一个数组,rating, 它扩展了原数组,末尾加了一个-1, 用于对最后一个元素进行判断。
对于栈底元素,即临界元素,其值应该等于左边得到的值 和通过栈的到的值中间最大的那一个。
还要在循环外, 对stack进行一次操作。
对最后一个点 还得讨论,
2)比前一个大 则为D(n -1) + 1
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的更多相关文章
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Leetcode Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【leetcode】Candy(hard) 自己做出来了 但别人的更好
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 【leetcode】Candy
题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 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 ...
- [LintCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- POJ - 1666 Candy Sharing Game
这道题只要英语单词都认得,阅读没有问题,就做得出来. POJ - 1666 Candy Sharing Game Time Limit: 1000MS Memory Limit: 10000KB 64 ...
- Candy Store
Candy Store Time Limit: 30000ms, Special Time Limit:75000ms, Memory Limit:65536KB Total submit users ...
随机推荐
- path 环境变量
path(环境变量)是dos以前的内部命令,windows继续沿用至今.用作运行某个命令的时候,本地查找不到某个命令或文件,会到这个声明的目录中去查找.一般设定java的时候为了在任何目录下都可以运行 ...
- (转载)浅谈我对DDD领域驱动设计的理解
原文地址:http://www.cnblogs.com/netfocus/p/5548025.html 从遇到问题开始 当人们要做一个软件系统时,一般总是因为遇到了什么问题,然后希望通过一个软件系统来 ...
- 状态模式(State)
状态模式,从字面意思上来讲应该是很简单的,就是针对实际业务上的内容,当类的内部的状态发生改变时,给出不同的响应体,就像现实中的人一样,早上没有吃饭,状态不好,上班.上课都会打哈欠,中午了,吃过午饭,又 ...
- 开始进入Windows Phone 8开发
和大家一起分享从零开始,入手Windows Phone 8开发,大家一起来吧!
- 【Qt】Qt Creator快捷键【转】
简介 Qt Creator中提供了各种快捷键来加快开发进程. 如果需要查看或自定义快捷键,选择工具->选项->环境->键盘.快捷键按类别列出,可以在过滤器(Filter)处输入命令名 ...
- ThreadLocal模式探索
一.首先,ThreadLocal模式使共享数据能多个线程被访问,每个线程访问的只是这个数据的副本,线程之间互不影响. 例子1: package Thread2; public class Counte ...
- Spannable相关方法
实现文本链接 其中tv是TextView类型的控件.只需写java代码即可实现链接,无需在xml文件中进行其他的设置. SpannableString spanTxt = new SpannableS ...
- ASP.NET中的母版页机制
项目中用到了母版页,由于好长时间没用了,不太熟悉起原理,在网上找了一下: http://www.cnblogs.com/_zjl/archive/2011/06/12/2078992.html 有时间 ...
- 【译】Android系统简介
简介 本文主要介绍Android的基础知识和体系结构,本文主题: 简介什么是Android,为什么开发者需要关注Android: Android体系结构(如Linux Kernel, Librari ...
- oracle窗口函数中range interval的使用
oracle窗口函数中range interval配合一般用来针对指定时间范围进行统计.其中range表示范围,between...and 表示之前的范围和之后的范围 , CURRENT ROW表示当 ...