【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 ...
随机推荐
- 迷你版Deferred
直接贴代码: /** * 迷你版的deferred */ function Deferred(func) { if (this instanceof Deferred === false) { ret ...
- 黑客攻防技术宝典Web实战篇(三)web攻击方式总结
web攻击的手段无非就是使服务器资源耗尽,使服务器无法接收正常请求. 一.DDos攻击 二.DRDos攻击 三.慢攻击 与Ddos攻击相反,慢攻击并不是以多取胜,而是靠保持连接.
- VTK初学一,b_PolyVertex_CellArray多个点的绘制
#ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...
- mysql 总结二(自定义存储过程)
mysql执行流程: sql命令--->mysql引擎-----(分析)---->语法正确-----(编译)--->可识别命令----(执行)---->执行结果---(返回)- ...
- DiscuzX 论坛首页 和 分 区设置版块横排
在论坛看到很多新手站长在咨询怎么样才可以设置和Discuz! 官方论坛首页一个分区下面横排3个板块或者更多呢?如下图: 下面我一起来操作下: 论坛 后台 论坛 板块管理 分区 编辑 图一: 图二: 说 ...
- 计算字符数组长度,用strlen 与 sizeof 的原理与区别
遇到个坑,定义了一个字符数组 unsigned ;i<;i++) { buff[i] = ; } 然后用串口发送函数: write(fd, buff, strlen(buff)); 却发现串口一 ...
- Code First02---CodeFirst配置实体与数据库映射的两种方式
Code First有两种配置数据库映射的方式,一种是使用数据属性DataAnnotation,另一种是Fluent API. 这两种方式分别是什么呢?下面进行一一解释: DataAnnotation ...
- MongoDB的索引(三)
MongoDB的索引: 1. _id索引 该索引是大多数集合默认创建的索引,也就是说用户每插入一个数据,MongoDB会自动生成一条唯一的_id字段. 2. 单键索引 单键索引是最普通的索引,它不会自 ...
- iOS的内购
内购: 向苹果付钱购买与APP的使用相关的产品(游戏中的道具,装备等): 苹果将收到的钱按比例,转给APP方: 不同于APP中的第三方支付(不经过苹果):
- [翻译]opengl扩展教程2
[翻译]opengl扩展教程2 原文地址https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/extensions_part2.php [ ...