/*
*/
#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. B/S 獲取客戶端Mac地址

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx. ...

  2. Java 取整

    向上取整用Math.ceil(double a) 向下取整用Math.floor(double a) 举例: public static void main(String[] args) throws ...

  3. addEventListener之handleEvent

    addEventListener() 方法是将指定的事件监听器注册到目标对象上,当该对象触发指定的事件时,指定的回调函数就会被执行.语法: element.addEventListener(type, ...

  4. java对mysql数据库进行单表筛选备份、还原操作

    最近在做的一个项目需要对mysql数据库中的单个表格进行备份 其中,一部分表格需要进行筛选备份(例如对最近插入的1000条记录进行备份) 思路:java调用系统命令完成备份操作 假设现在有数据库tes ...

  5. JavaScript 的 OOP 功能解析

    根据JavaScript创始人Brandon Eich 自己的说法,JavaScript 最好的语言构造是: 函数是一等公民 (first class functions) 闭包 (closure) ...

  6. ActiveX控件资料

    Visual Studio 2008(c#)开发ActiveX控件及制作CAB包总结(1) 分类: C#2011-05-27 15:50 403人阅读 评论(0) 收藏 举报 c#stringhook ...

  7. $_CFG = load_config(); /* 载入系统参数 */

    ecshop 中$_CFG数组主要是放置一些系统参数,并且全站共享的数据,在使用的时候,ecshop里面常常以$GLOBALS['_CFG']全局变量的模式来处理. ecshop 的$GLOBALS[ ...

  8. MySql存储过程—2、第一个MySql存储过程的建立

    看看如何创建一个存储过程.虽然通过命令行可以创建,但基本通过MySQL提供的Query browser来创建. 1.首先我们通过Administrator在test数据库中创建一个简单的表名叫”pro ...

  9. php网站判断用户是否是手机访问的方法

    PHP网站判断用户是否用手机访问,如果是手机的话,就跳转到指定的手机友好页面.随着移动设备的普及,网站也会迎来越来越多移动设备的访问.用适应PC的页面,很多时候对手机用户不友好,那么有些时候,我们需要 ...

  10. python运维开发之第六天

    Python面向对象 python从设计之初就已经是一门面向对象的语言,在python中创建一个类和对象很容易. 面向对象简介:类(class),类变量,object(基类),实例变量,构造函数,封装 ...