/*
*/
#include<iostream>
#include<vector>
//#include<algorithm>
#include <windows.h> using namespace std;
#define STOP system("pause");
#if 1
class Solution {
public:
int candy(vector<int> &ratings) {
int len = ratings.size();
int *candy = new int[len]{1};//仅仅有candy[0]=1,others = 0;
//vector<int> candy(len, 1);
for (int i = 1; i < len; i++){
if (ratings[i] > ratings[i - 1]){
candy[i] = candy[i - 1] + 1;
}
else candy[i] = 1;
}
for (int i = len - 2; i >= 0; i--){
if (ratings[i] > ratings[i + 1]){
candy[i] =max(candy[i], candy[i + 1] + 1);
}
}
int res{};
for (int i = 0; i < len; i++){
res += candy[i];
}
return res;
}
};
#elif 0
class Solution {
public:
int candy(const vector<int>& ratings) {
vector<int> f(ratings.size());
int sum = 0;
for (int i = 0; i < ratings.size(); ++i)
sum += solve(ratings, f, i);
return sum;
}
int solve(const vector<int>& ratings, vector<int>& f, int i) {
if (f[i] == 0) {
f[i] = 1;
if (i > 0 && ratings[i] > ratings[i - 1])
f[i] = max(f[i], solve(ratings, f, i - 1) + 1);
if (i < ratings.size() - 1 && ratings[i] > ratings[i + 1])
f[i] = max(f[i], solve(ratings, f, i + 1) + 1);
}
return f[i];
}
};
#elif 0
class Solution {
public:
int candy(vector<int> &ratings) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int len = ratings.size();
int nCandyCnt = 1;///Total candies
int nSeqLen = 0; /// Continuous ratings descending sequence length
int nPreCanCnt = 1; /// Previous child's candy count
int nMaxCntInSeq = nPreCanCnt;
//if (ratings.begin() != ratings.end())
//for (vector<int>::iterator i = ratings.begin() + 1; i != ratings.end(); i++)
for (int i = 1; i < len; 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 (ratings[i] < ratings[i - 1])
{
//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 = 1;
}
else
{
if (ratings[i] > ratings[i - 1])
{
nPreCanCnt++;
}
else
{
nPreCanCnt = 1;
}
nCandyCnt += nPreCanCnt;
nSeqLen = 0;
nMaxCntInSeq = nPreCanCnt;
}
} return nCandyCnt;
}
};
#endif
void test0(){
vector<int> a{ 0, 1, 3,1,2,3,4,5,6,5,4,3,2,1 };
int r[10]{1, 2};
Solution ss;
ss.candy(a);
} int main(){
LARGE_INTEGER nFreq;
LARGE_INTEGER nBeginTime;
LARGE_INTEGER nEndTime;
double time;
QueryPerformanceFrequency(&nFreq);
QueryPerformanceCounter(&nBeginTime);
test0();
QueryPerformanceCounter(&nEndTime);
time = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) / (double)nFreq.QuadPart;
cout << time << endl;
STOP;
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

*candy——leetcode的更多相关文章

  1. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  2. Candy leetcode java

    题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...

  3. Candy [leetcode] O(n)时间复杂度,O(1)空间复杂度的方法

    对于ratings[i+1],和ratings[i]的关系有下面几种: 1. 相等.相等时ratings[i+1]相应的糖果数为1 2.ratings[i + 1] > ratings[i].在 ...

  4. candy leetcode C++

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  5. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  6. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  7. [LeetCode] Candy 分糖果问题

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  8. 【LEETCODE OJ】Candy

    Problem link: http://oj.leetcode.com/problems/candy/ Suppose we are given an array R[1..N] that are ...

  9. (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重

    原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...

随机推荐

  1. jQuery ajax - serialize() 方法

    http://www.jb51.net/article/60849.htm http://www.w3school.com.cn/jquery/ajax_serialize.asp

  2. C# 、winform 添加皮肤后(IrisSkin2) label设置的颜色 无法显示

    C# .winform 添加皮肤后(IrisSkin2) label设置的颜色 无法显示 解决方法一:设置label的Tag属性值与skinEngine的DisableTag属性值相同即可.默认值是9 ...

  3. openssl提取pfx证书密钥对

    刚做银联的项目,对方给了1.pfx和1.cer两个测试文件,总结一下利用这两个文件提取出文本 银联提供两个测试证书  1.pfx 和 1.cer . 其中 pfx证书包含RSA的公钥和密钥;cer证书 ...

  4. 【转】POJ题目分类

    初级:基本算法:枚举:1753 2965贪心:1328 2109 2586构造:3295模拟:1068 2632 1573 2993 2996 图:最短路径:1860 3259 1062 2253 1 ...

  5. (兼容IE6)又一个提示框思密达,腾讯UED 201401242352

    找乐子 仿QQ空间的,先来看下,别嫌代码垃圾,业余菜鸟一个,用到的话就当个乐子就行了 注意: 因为有同学说需要IE6便做了一下. 已经处理了IE6,可测试. 腾讯的东西,感觉还好吧:) 使用方法老简单 ...

  6. a便签 rel属性改变链接打开页面的方式

    <body> XHTML: <a href="http://www.baidu.com" rel="external">Baidu &l ...

  7. C#程序中将图片转换为二进制字符串,并将二进制字符串转换为图片

    /// <summary> /// 将图片以二进制流 /// </summary> /// <param name="path"></pa ...

  8. Altera quartus II遇到的问题

    编译时提示: Warning (13024): Output pins are stuck at VCC or GND Warning (13410): Pin "SCLK" is ...

  9. spring restful 中文乱码问题

    进行如下配置: @RequestMapping( value="/zzs/xgm", produces="application/json;charset=utf-8&q ...

  10. 【JavaScript】关于JavaScript

    1. Language 2. DOM 3. Library 4. Framework