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 ...
随机推荐
- Eclipse 快捷键 快捷输入
快捷键: 1. ctrl+shift+r:打开资源 这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask名中的前几个字母,比如appli ...
- 6.struts登陆页面的演示
1.创建一个web project "Struts_1" 添加struts的jar包 --在项目文件右键->myeclipse->add struts... ...
- 字符串转成int数组
package lianxi; import java.awt.image.ConvolveOp; public class ZhengshuShuzu { public static void ma ...
- jQuery 添加元素和删除元素
jQuery - 添加元素 append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() - 在被选元 ...
- AngularJS(11)-API
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Sending Email from mailx Command in Linux Using Gmail’s SMTP
The mailx or mail command in Linux is still providing service for guys like me, especially when we n ...
- php + apache + mysql环境搭建
别人写的很好,若是不改变php默认访问路径的话,能够成功搭建是没问题的 http://www.cnblogs.com/pharen/archive/2012/02/06/2340628.html
- WCF全面解析第二章 地址(Adress)
2.1 统一资源标识(URL) 2.1.1 Http/Https 2.1.2 Net.TCP 2.1.3 Net.Pipe WCF只将命名管道专门用于同一台机器的跨进程通信. 2.1.4 Net.Ms ...
- JQuery解析JSon
JsonCreatet.ashx页面 JSonAnalysis.aspx测试页面 一般处理程序中使用Newtonsoft.Json来序列化json 页面使用Jquery 来解析Json数据 Jquer ...
- linux设备驱动模型
尽管LDD3中说对多数程序员掌握设备驱动模型不是必要的,但对于嵌入式Linux的底层程序员而言,对设备驱动模型的学习非常重要. Linux设备模型的目的:为内核建立一个统一的设备模型,从而又一个对系统 ...