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 ...
随机推荐
- 安装PHP软件
安装PHP软件 ① tar -zxvf php-5.2.5.tar.gz ② cd php-5.2.5 ③ 使用configure配置安装信息(最重要) ./configure \ --prefix= ...
- CICS&&XA
CICS (Customer Information Control System) 是IBM 公司的强大主机交易服务器.集成平台,在全球C.C++.COBOL等交易中间件市场上占有绝大多数客户.CI ...
- ServletContext的用途
安装在一个服务器中的一个特定URL名字空间(比如,/myapplication)下的所有Servlet,JSP,JavaBean等Web部件的集合构成了一个Web的应用,每一个Web应用(同一JVM) ...
- 【风马一族_Android】Android 前端内容1
Android 前端内容 4.1 View 类概述 4.1.1 关于 View //类型说明 view(视图)指的是用户界面组件的基本构建基块.一个视图占据屏幕上的矩形区域,负责绘图和事件处理.视图是 ...
- Linux多命令协作:管道及重定向
- nginx安装总结
对于nginx作为负载均衡服务器时的安装需要安装rewrite模块需要的pcre()库,gzip模块需要zlib库,ssl模块需要openssl库,对此依赖安装有很多种处理方式,以下简单总结: 通过源 ...
- Hadoop介绍及最新稳定版Hadoop 2.4.1下载地址及单节点安装
Hadoop介绍 Hadoop是一个能对大量数据进行分布式处理的软件框架.其基本的组成包括hdfs分布式文件系统和可以运行在hdfs文件系统上的MapReduce编程模型,以及基于hdfs和MapR ...
- mysql5.7.12安装过程中遇到的一些问题
在安装mysql-5.7.12-winx64中遇到的问题总结 1.该版本的mysql解压后的文件夹里没有data文件(切记自己添加data,自己添加的文件可能出现的问题是文件里的文件会缺失) 我在使用 ...
- ERROR 23 (HY000) at line 29963: Out of resources when opening file
在还原数据库时报错,报错信息如下:(库中的表比较多) ERROR 23 (HY000) at line 29963: Out of resources when opening file 解决方法: ...
- ios里的UIActionSheet的使用
class ViewController: UIViewController,UIActionSheetDelegate{ @IBOutlet weak var label1: UILabel! @I ...