Leetcode#135 Candy
遍历所有小孩的分数
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的更多相关文章
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
- leetcode 135. Candy ----- java
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Java for LeetCode 135 Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- [leetcode] 135. Candy (hard)
原题 前后两遍遍历 class Solution { public: int candy(vector<int> &ratings) { vector<int> res ...
- [Leetcode 135]糖果分配 Candy
[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【LeetCode】135. Candy
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 135. Candy
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
随机推荐
- linux中的虚拟化网络模型及各种模型实现
第一种隔离模型: Guest1和Guest2都为虚拟机. 首先要了解在linux中的虚拟机的网卡都包含前半段和后半段,前半段在虚拟机上,后半段在宿主机上,这里以centos6为例,上图 eth0为Gu ...
- jQuery打造用户注册时获取焦点文本框出现提示jquery表单特效
jQuery打造用户注册时获取焦点文本框出现提示效果的jquery表单特效 当获取焦点时,会分别的弹出相应的信息提示框,失去焦点就会隐藏提示信息. 效果兼容性很高,适用浏览器:IE6.IE7.IE8. ...
- Java实现抽奖游戏
代码如下: import java.io.*; public class PresentDemo { /** * @param args */ public static void main(Stri ...
- div+css的优势在哪?
1.符合W3C标准.微软等公司都是他的支持者. 2.所搜引擎更加友好. 3.样式调整更加方便. 4.css简洁的代码,减少了带宽. 5.表现和结构分离.在团队开发中更容易分工 并不是取代table,t ...
- POJ-1579
#include <stdio.h> #include <stdlib.h> ][][]; int w(int a,int b,int c){ ||b<=||c<= ...
- android 中单选和复选框监听操作
单选按钮RadioGroup.复选框CheckBox都有OnCheckedChangeListener事件,我们一起了解一下. package com.genwoxue.oncheckedchange ...
- C# Sql 触发器
触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 触发器对表进行插入.更新.删 ...
- sql server 判断是否存在数据库,表,列,视图
1 判断数据库是否存在if exists (select * from sys.databases where name = '数据库名') drop database [数据库名] 2 判断表 ...
- [Letcode] 1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- git的初步使用
1.在GitHub上建立项目登录GitHub后,你可以在右边靠中那里找到一个按钮“New Repository”,点击过后,填入项目名称.说明和网址过后就可以创建了,然后会出现一个提示页面,记下类似g ...