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 ...
随机推荐
- php获取图片宽高等属性
<?php function getImageInfo($image) { $imageInfo = getimagesize($image); if ($imageInfo ! ...
- 远程连接数据库(通过pgAdmin)
1.编辑/var/lib/pgsql/data/pg_hba.conf,增加语句 host all all 192.168.105.225/36 trust 让数据库接受网络 192.168.105 ...
- Python学习教程(learning Python)--1.2Python输入输出与处理
一般在做Python程序设计时,通常程序的结构由三部分组成: 输入语句,主要用于输入数据: 数据处理语句,一般对数据进行算术.逻辑等运算处理操作: 输出语句,将输入或者处理结果输出,用于与用户交互. ...
- 使用MSYS2编译64位gvim
1. 下载安装MSYS2 在https://msys2.github.io/下载MSYS2,推荐下载x86-64版,此版本内置了MinGW32与MinGW64 安装后首先更新MSYS2系统,顺序执行下 ...
- git的工作流程(分支合并)
git支持很多种工作流程,我们采用的一般是这样,远程创建一个主分支,本地每人创建功能分支,日常工作流程如下: 去自己的工作分支$ git checkout work 工作.... 提交工作分支的修改$ ...
- oracle-审计导数
1.因审计需求,需要将MySQL.Oracle数据库中需要的表数据导入到SqlSERVER进行审计. 2.之前的方法: A. oracle组将表dump下来,进行压缩,传送到oracle导数服务器 ...
- SystemServer相关
SystemServer分析 由Zygote通过Zygote.forkSystemServer函数fork出来的.此函数是一个JNI函数,实现在dalvik_system_Zygote.c中. 1.S ...
- hdu 1427 速算24点
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1427 速算24点 Description 速算24点相信绝大多数人都玩过.就是随机给你四张牌,包括A( ...
- hdu 2544 最短路
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shi ...
- .net 的生成操作
生成操作(BuildAction) 属性:BuildAction 属性指示 Visual Studio .NET 在执行生成时对文件执行的操作. BuildAction 可以具有以下几个值之一: 无( ...