【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 ...
随机推荐
- SQLite初试...
string dataPath = "../../UserData.dbx"; //System.IO.Directory.GetCurrentDirectory() + &quo ...
- UIView不接受触摸事件的三种情况
1.不接收用户交互 userInteractionEnabled = NO 2.隐藏 hidden = YES 3.透明 alpha = 0.0 ~ 0.01 4. 如果子视图的位置超出了父视图的有效 ...
- 【bzoj4034】[HAOI2015]树上操作
题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都 ...
- PHP函数preg_replace() 正则替换所有符合条件的字符串
PHP preg_replace() 正则替换,与JavaScript 正则替换不同,PHP preg_replace() 默认就是替换所有符号匹配条件的元素. preg_replace (正则表达式 ...
- git之remote branch controller(远程分支控制)
1.创建本地分支 git branch //查看远程分支 git checkout -b branch_name //创建远程分支 在查看分支git branch 2.将分支提交到远程仓库 此时远程 ...
- C#获取文件/字节数组MD5值方法
找了很多,就这个管用,有时间好好研究一番 public static string GetMD5Hash(string fileName) { try { FileStream file = new ...
- 安卓手机当Mac、Win和Linux外置WIFI网卡
Android手机都有USB共享网络的功能 ,利用此功能可以将网络共享给电脑 基本原理: 手机连接WIFI,开启USB网络共享,mac笔记本上网 (两款手机示意图) 博主实践证明: win10与Ubu ...
- SQL双重游标(双重循环)--笔记
declare )='', )='', )='' --创建游标 declare @cursor cursor --设定游标欲操作的数据集 set @cursor=cursor for select s ...
- 如何开启telnet 23端口
netstat -tnl|grep 23 查看23端口是否开启 或者 chkconfig --list|grep telnet 检查telnet状态 如果关闭状态, 开启:chkconfig --le ...
- (二)js下拉菜单
默认的select标签比较难看,UI比较漂亮,如果想要实现UI上的下拉样式,好像必须用js写select,从网上拷贝而且修改了一个下拉框,为了方便以后引用所以记录下来. /* diy_select * ...