【leetcode】Candy(hard) 自己做出来了 但别人的更好
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?
注意,像 6 5 5 4 这样的 可以分 2 1 2 1 就是数字相同的不需要糖果数相同
我的思路,分别用两个vector存储递增和递减所需要的糖果,最终结果取两个的最大值
如 权重 6 6 1 5 4 4 3 7 4 2 2 2 5 4 3 5
找递增 1 1 1 2 1 1 1 2 1 1 1 1 2 1 1 2 从前向后找,初始都是1,遇到递增的就加上他前面查找的数字
找递减 1 2 1 2 1 2 1 3 2 1 1 1 3 2 1 1 从后向前找,初始都是1,遇到递增的就加上他前面查找的数字
最终 1 2 1 2 1 2 1 3 2 1 1 1 3 2 1 2
int candy(vector<int> &ratings) {
int ans = ;
vector<int> candysUp(ratings.size(), );
vector<int> candysDown(ratings.size(), );
for(int i = ; i < ratings.size(); i++) //查找递增
{
if(ratings[i] > ratings[i - ])
{
candysUp[i] += candysUp[i - ];
}
}
for(int i = ratings.size() - ; i >= ; i--) //查找递减
{
if(ratings[i + ] < ratings[i])
{
candysDown[i] += candysDown[i + ];
}
} for(int i = ; i < ratings.size(); i++)
{
ans += max(candysUp[i], candysDown[i]);
}
return ans;
}
大神只循环一遍的思路:
其实整体思想跟上面一样,但是上面需要循环很多次是因为递增和递减分别处理。因为递减时不知道最大的数应该加几。
大神的思路是递增的就像上面那样加,但是递减的,先都加1,如果后面仍是递减的,再把之前少加的给加回来(如前面有nSeqLen个数字少加了1,就把答案直接多加一个nSeqLen)。
//大神循环一遍的代码
int candy2(vector<int> &ratings) {
int nCandyCnt = ;///Total candies
int nSeqLen = ; /// Continuous ratings descending sequence length
int nPreCanCnt = ; /// Previous child's candy count
int nMaxCntInSeq = nPreCanCnt;
if(ratings.begin() != ratings.end())
{
nCandyCnt++;//Counting the first child's candy.
for(vector<int>::iterator i = ratings.begin()+; i!= ratings.end(); i++)
{
// if r[k]>r[k+1]>r[k+2]...>r[k+n],r[k+n]<=r[k+n+1],
// r[i] needs n-(i-k)+(Pre's) candies(k<i<k+n)
// But if possible, we can allocate one candy to the child,
// and with the sequence extends, add the child's candy by one
// until the child's candy reaches that of the prev's.
// Then increase the pre's candy as well. // if r[k] < r[k+1], r[k+1] needs one more candy than r[k]
if(*i < *(i-))
{
//Now we are in a sequence
nSeqLen++;
if(nMaxCntInSeq == nSeqLen)
{
//The first child in the sequence has the same candy as the prev
//The prev should be included in the sequence.
nSeqLen++;
}
nCandyCnt+= nSeqLen;
nPreCanCnt = ;
}
else
{
if(*i > *(i-))
{
nPreCanCnt++;
}
else
{
nPreCanCnt = ;
}
nCandyCnt += nPreCanCnt;
nSeqLen = ;
nMaxCntInSeq = nPreCanCnt;
}
}
}
return nCandyCnt;
}
【leetcode】Candy(hard) 自己做出来了 但别人的更好的更多相关文章
- 【leetcode】Best Time to Buy and Sell 3 (hard) 自己做出来了 但别人的更好
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现
[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- [LeetCode] Candy Crush 糖果消消乐
This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...
- leetcode — candy
/** * Source : https://oj.leetcode.com/problems/candy/ * * There are N children standing in a line. ...
- [leetcode]Candy @ Python
原题地址:https://oj.leetcode.com/problems/candy/ 题意: There are N children standing in a line. Each child ...
- Leetcode Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- LeetCode: Candy 解题报告
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- [Leetcode] candy 糖果
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
随机推荐
- SqlServer代理执行[分发清除: distribution] 无法删除快照文件
每天偶尔检查数据库作业是否失败,发现有错误 select top 10 job_id,run_date,run_time,run_duration,step_name,message from ms ...
- 编译本地64位版本的hadoop-2.6.0
官方提供的hadoop-2.x版本貌似都是32位的,在64位机子下使用可能会报错,最好使用官方提供的源码进行本地编译,编译成适合本地硬件环境的64位软件包. 关于native Hadoop是使用J ...
- codevs5164 逆波兰表达式
题目描述 Description 逆波兰表达式是一种把运算符前置的算术表达式(又叫前缀表达式),例如普通的表达式2 + 3的逆波兰表示法为+ 2 3.逆波兰表达式的优点是运算符之间不必有优先级关系,也 ...
- poj 3744 Scout YYF I(概率dp,矩阵优化)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5020 Accepted: 1355 Descr ...
- html 补充
替换文本属性(Alt)alt 属性用来为图像定义一串预备的可替换的文本.替换文本属性的值是用户定义的.<img src="boat.gif" alt="Big Bo ...
- 2015年最有价值的30个响应式WORDPRESS主题
http://www.chinaz.com/design/2015/0521/408204.shtml 必须承认,Wordpress依然是目前最流行.最易用的内容管理系统,合理地使用Wordpress ...
- hibernate 的三种状态 如何转化的。
1. 临时状态 由 new命令开辟内存空间的java对象,例如: User user=new User(); 临 时对象在内存孤立存在,它是携带信息的载体,不和数据库的数据有任何关联关系. 2. ...
- Java 7 Concurrency Cookbook 翻译 第一章 线程管理之三
五.睡眠和唤醒一个线程 有时,你会想要在一段特定的时间后再去中断线程的运行.举个例子,程序中的一个线程每一分钟检查一次传感器的状态,剩余的时间,线程应该处于空闲的状态.在这段空闲时间里,线程不会使用计 ...
- CSU 1120 病毒(DP)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120 解题报告:dp,用一个串去更新另一个串,递推方程是: if(b[i] > a ...
- Git秘籍:在 Git 中进行版本回退
导读 在这篇文章中,你将学到如何查看项目中的历史版本,如何进行版本回退,以及如何创建 Git 分支以便你可以大胆尝试而不会出现问题.快来试试吧. 在你的 Git 项目的历史中,你的位置就像是摇滚专辑中 ...