/*
*/
#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. 新建android系统服务

    一.Android系统服务 Android提供了很多系统服务:如ActivityManger,PowerManger,WindowManger,WifiManger等等. 这些服务都是系统启动开始就一 ...

  2. 【转】iOS开发系列--数据存取

    原文: http://www.cnblogs.com/kenshincui/p/4077833.html#SQLite 概览 在iOS开发中数据存储的方式可以归纳为两类:一类是存储为文件,另一类是存储 ...

  3. OC语法简写

    NSNumber [NSNumber numberWithInt:666] 等价于 @666 [NSNumber numberWithLongLong:666ll] 等价于 @666ll [NSNum ...

  4. 【转】lua random()

    先来看看这两段代码: ① math.randomseed(os.time())for i=1,10 do n = math.random(10) print(n) 运行结果是: 63210754341 ...

  5. c#中使用ABCpdf处理PDF

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  6. Excel报表

    Excel报表 1.Excel报表导入到GridView protected void Page_Load(object sender, EventArgs e) { string path = Se ...

  7. 移除IOS下按钮的原生样式

    写WAP页面的时候  一定要加上这组样式,以避免在IOS下面按钮被系统原生样式影响 input,textarea {outline-style:none;-webkit-appearance:none ...

  8. Git 远程仓库的管理和使用

    要参与任何一个 Git 项目的协作,必须要了解该如何管理远程仓库.远程仓库是指托管在网络上的项目仓库,可能会有好多个,其中有些你只能读,另外有些可以写.同他人协作开发某 个项目时,需要管理这些远程仓库 ...

  9. Jquery中$.post()与$.get()区别

    1:GET访问 浏览器 认为 是等幂的 就是 一个相同的URL 只有一个结果[相同是指 整个URL字符串完全匹配] 所以 第二次访问的时候 如果 URL字符串没变化 浏览器是 直接拿出了第一次访问的结 ...

  10. python设计模式之装饰器模式

    装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...