原题地址

遍历所有小孩的分数

1. 若小孩的分数递增,分给小孩的糖果依次+1
2. 若小孩的分数递减,分给小孩的糖果依次-1
3. 若小孩的分数相等,分给小孩的糖果设为1

当递减序列结束时,如果少分了糖果,就补上,如果多分了糖果,就减掉

究竟补多少或减多少,这很容易计算,不啰嗦了。

时间复杂度:O(n)

代码:

 int candy(vector<int> &ratings) {
int n = ratings.size();
int sum = ;
int len = ;
int prev = ; for (int i = ; i < n; i++) {
if (ratings[i] == ratings[i - ]) {
prev = ;
len = ;
sum += prev;
}
else if (ratings[i] > ratings[i - ]) {
prev++;
len = ;
sum += prev;
}
else if (ratings[i] < ratings[i - ]) {
prev--;
len++;
sum += prev;
if (i == n - || ratings[i] <= ratings[i + ]) {
sum += prev < ? ( - prev) * (len + ) : ( - prev) * len;
prev = ;
}
}
} return sum;
}

Leetcode#135 Candy的更多相关文章

  1. LeetCode 135 Candy(贪心算法)

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

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

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

  3. leetcode 135. Candy ----- java

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

  4. Java for LeetCode 135 Candy

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

  5. [leetcode] 135. Candy (hard)

    原题 前后两遍遍历 class Solution { public: int candy(vector<int> &ratings) { vector<int> res ...

  6. [Leetcode 135]糖果分配 Candy

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

  7. 【LeetCode】135. Candy

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

  8. [LeetCode][Java]Candy@LeetCode

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

  9. 135. Candy

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

随机推荐

  1. Mongod(5):启动命令mongod参数说明

    Mongodb启动命令mongod参数说明(http://blog.csdn.net/fdipzone/article/details/7442162) mongod的主要参数有: 基本配置 ---- ...

  2. [译]MongoDb生产环境注意事项

    译注: 本文是翻译MongoDB Manuel中的MongoDB Production Notes一节内容.这节内容重点关注生产环境中影响性能和可靠性的各种注意事项,值得正在部署MongoDB的工作者 ...

  3. 一个伪ajax图片上传代码的例子

    一个伪ajax图片上传实现代码. 复制代码代码如下: <?php  if($_FILES){  ?>  <script>  window.parent.ajaxUploadPi ...

  4. php static延迟静态绑定

    如果你是一个懒惰的程序员,你看到以下代码可能会恼火 abstract class U{ } class u1 extends U{ public static function create(){ r ...

  5. Google搜索镜像

    From:http://www.cnblogs.com/killerlegend/p/3783744.html Date:2014.6.12 By KillerLegend Google 搜索:htt ...

  6. 浅谈DEs,AES

    1. AES加密,相对比较简单,之前已经配置好工具类. package com.bbguoxue.poetry.util; import java.security.SecureRandom; imp ...

  7. Java 第七天 动态代理

    代理类需实现InvocationHandler接口: public interface InvocationHandler { public Object invoke(Object proxy,Me ...

  8. 010-python基础-数据类型-字符串操作

    1.移除空白 username.strip() 2.分割 names = "alex,jack,rain" names_1 = names.split(",") ...

  9. Python脚本控制的WebDriver 常用操作 <十二> send_keys模拟按键输入

    下面将使用WebDriver中的send_keys来模拟键盘按键输入 测试用例场景 send_keys方法可以模拟一些组合键操作: ctrl+a ctrl+c ctrl+v 等. 另外有时候我们需要在 ...

  10. 从0 开始 WPF MVVM 企业级框架实现与说明 ---- 第四讲 WPF中 ControlTemplate

    上讲我们介绍了DataTemplate,现在我们就介绍下ControlTemplate,可能后面大多在编码时候会出现一些英文,工作习惯,请见谅. ControlTemplate: 控件的外观,也就是控 ...