/*
*/
#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. xcode升级插件失效修复

    每次xcode升级以后,插件都会失效.可以通过一行命令解决这个问题. 摘自传人的博客 find ~/Library/Application\ Support/Developer/Shared/Xcod ...

  2. ASP.NET菜鸟之路之Seesion小例子

    背景 我是一个ASP.NET菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了. Session介绍 Session保存特定用户相 ...

  3. 【USACO 2.4.3】牛的旅行

    [描述] 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...

  4. linux c下几种定时器实现

    1.alarm n秒后触发一次,不是循环的2.setitimer 可以发出3种信号给自己,3.timerfd 这个接口基于文件描述符,通过文件描述符类似epoll那种的可读事件进行超时通知,能够被用于 ...

  5. bash shell学习-shell基础 (笔记)

    When you hoist the sails to cross the sea, you willride the wind and cleave the waves. "长风破浪会有时 ...

  6. js 得到当前季度

    Date.prototype.getQuarter = function() { var month = this.getMonth(); if(month < 3) { return '第一季 ...

  7. java获得系统当前日期

    package com.web.test; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...

  8. jquery实现页面置顶功能代码

    <html> <head> <title></title><script type='text/javascript> //回到顶部功能 f ...

  9. mysql的sql优化案例

    前言 mysql的sql优化器比较弱,选择执行计划貌似很随机. 案例 一.表结构说明mysql> show create table table_order\G***************** ...

  10. linux 命令——PS命令

    首先要明白什么是进程进程是一个正在运行的程序.也就是说在Linux系统上运行的所有东西都是进程.和进程相比较,作业是一系列按一定顺序执行的命令.一条简单的命令可能会涉及多个进程,尤其是当使用管道和重定 ...