对于ratings[i+1],和ratings[i]的关系有下面几种:

1. 相等。相等时ratings[i+1]相应的糖果数为1

2.ratings[i + 1] > ratings[i]。在这样的情况下,要寻找以ratings[i]開始的递增序列。

3.ratings[i + 1] < ratings[i]。在这样的情况下,要寻找以ratings[i]開始的递减序列。

对于随意一个递增序列 [2 3 4 5 6] 相应的糖果数为 [1 2 3 4 X]

对于随意一个递减序列[6 5 4 3 2]相应的糖果数为[X 4 3 2 1]

X为递增和递减序列交际处的元素相应糖果数。应该是递增序列长度和递减序列长度中较大的值

代码例如以下:

    int candy(vector<int> &ratings) {
if (ratings.size() == 0) return 0;
int sum = 0;
int candyNum = 1;
for (int i = 0; i < ratings.size() - 1;)
{
if (ratings[i] == ratings[i + 1])
{
//if is the same rating, reset candy num. ie: 1 3 3, for the 2nd 3, candy num is 1
sum += candyNum;//add current candy num
i++;
candyNum = 1;//set next candy num to 1
}
else if (ratings[i] < ratings[i + 1])
{
// find ascending sequence, until i is the end of sequence. ie: 1 2 3 1, ratings[i] is 3
for (;i < ratings.size() - 1 && ratings[i] < ratings[i + 1]; i++) sum += (candyNum++);
}
else if (ratings[i] > ratings[i + 1])
{
// find descending sequence, until i is the end of sequence. ie: 3 2 1 3, rating[i] is 1
int decCount = 1;
for (; i < ratings.size() - 1 && ratings[i] > ratings[i + 1]; i++) sum += (decCount++);
sum += max(candyNum, decCount);//add first element of the sequence
//remove last element of the sequence, as i is the end of sequence, and i's candy num shouldn't be calculated into sum
sum --;
candyNum = 1;
}
}
sum += candyNum;
return sum;
}

Candy [leetcode] O(n)时间复杂度,O(1)空间复杂度的方法的更多相关文章

  1. [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现

    [LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现 原题: There are N children standing in a line. ...

  2. 某整形数组中除了两个单身整数外, 其余的整数都是成对出现的, 利用C/C++代码求出这两个单身整数。 要求: 时间复杂度o(n), 空间复杂度o(1)------某公司招聘试题

    先看看这个题目:某整形数组中除了两个单身整数外, 其余的整数都是成对出现的, 利用C代码求出这两个单身整数. 要求: 时间复杂度o(n), 空间复杂度o(1). 我们先用最傻瓜的方式来做吧: #inc ...

  3. [LeetCode][Java]Candy@LeetCode

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

  4. 时间复杂度On和空间复杂度O1是什么意思?

    (1).把输入规模看成x轴,所花时间/空间看成y轴 O(n)就是y=x,y随x的增长而线性增长.也就是成正比,一条斜线. O(1)就是y=1,是一个常量,不管x怎么变,y不变,一条与x轴平行的线. ( ...

  5. *candy——leetcode

    /* */ #include<iostream> #include<vector> //#include<algorithm> #include <windo ...

  6. Candy leetcode java

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

  7. candy leetcode C++

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

  8. 反转链表,时间复杂度O(n),空间复杂度O(1)

    原理:使用三个指针,p,q指向交换的元素,r指向后续元素 代码如下: class Node{ int data; Node next; Node(int data){ this.data=data; ...

  9. 时间复杂度O(n),空间复杂度O(1)解斐波那契数列

    #include <stdio.h> #include <iostream> using namespace std; long long fibs1(int in_iN) { ...

随机推荐

  1. Linux命令(002) -- free

    一.准备知识 Linux和Windows系统在内存管理机制方面有很大的不同.在Linux中经常发现空闲内存很少,似乎所有的内存都被系统占用了,表面感觉是内存不够用了,其实不然.这是Linux内存管理的 ...

  2. 什么是mycat?

    Mycat关键特性 关键特性 支持SQL92标准 支持MySQL.Oracle.DB2.SQL Server.PostgreSQL等DB的常见SQL语法 遵守Mysql原生协议,跨语言,跨平台,跨数据 ...

  3. MySql(二):常见的那些个约束

    今天总结一下mysql当中的常见约束吧! 那什么是约束呢?通俗点讲,约束就是限定指定字段的存放规则! ● 主键约束(Primary Key) ● 外键约束(Foreign Key) ● 非空约束(No ...

  4. HashMap的尾部遍历问题--Tail Traversing

    在看网上HashMap的resize()设计时,提到尾部遍历.   JDK1.7的HashMap在实现resize()时,新table[]的列表采用LIFO方式,即队头插入.这样做的目的是:避免尾部遍 ...

  5. SAS进阶《深入解析SAS》之Base SAS基础、读取外部数据到SAS数据集

    SAS进阶<深入解析SAS>之Base SAS基础.读取外部数据到SAS数据集 前言:在学习完<SAS编程与商业案例>后,虽然能够接手公司的基本工作,但是为了更深入的SAS学习 ...

  6. CSS——dispaly、overflow、visibility、opacity

    隐藏盒子: 1.overflow:hidden;             隐藏盒子超出的部分. 2.display: none;                    隐藏盒子,而且不占位置.(用的最 ...

  7. 如何用java生成随机验证码

     1.VerifyCode 类:   1 package com.HRuinger.enity;                          ImageIO.write(image, " ...

  8. [Windows Server 2003] IIS自带FTP安装及配置方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:IIS6.0自 ...

  9. 6、scala Map和Tuple

    1.  创建Map 2.访问Map元素 3.修改Map元素的值 4.遍历Map 5.SortedMap和LinkedHashMap 6.Map的元素类型Tuple 1.  创建Map 创建不可变的Ma ...

  10. What is gradle sync in Android Studio?

    What is it? And what does it do? Gradle sync is a gradle task that looks through all of your depende ...