candy leetcode C++
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy. Children with a higher rating get more candies than their neighbors. What is the minimum candies you must give?
C++
class Solution {
public:
int candy(vector<int> &ratings) {
int len=ratings.size();
if(len==1) return 1;
int sum=0;
vector<int> v(len,1);
for(int i=1;i<len;i++){
if(ratings[i] > ratings[i-1])
v[i]=v[i-1]+1;
}
for(int i=len-2;i>=0;i--){
if(ratings[i] > ratings[i+1] && v[i] <= v[i+1])
v[i]=v[i+1]+1;
}
for(int i=0;i<len;i++){
sum+=v[i];
}
return sum;
}
};
candy leetcode C++的更多相关文章
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- *candy——leetcode
/* */ #include<iostream> #include<vector> //#include<algorithm> #include <windo ...
- Candy leetcode java
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
- Candy [leetcode] O(n)时间复杂度,O(1)空间复杂度的方法
对于ratings[i+1],和ratings[i]的关系有下面几种: 1. 相等.相等时ratings[i+1]相应的糖果数为1 2.ratings[i + 1] > ratings[i].在 ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 【LEETCODE OJ】Candy
Problem link: http://oj.leetcode.com/problems/candy/ Suppose we are given an array R[1..N] that are ...
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
随机推荐
- zt:我使用过的Linux命令之ar - 创建静态库.a文件
我使用过的Linux命令之ar - 创建静态库.a文件 本文链接:http://codingstandards.iteye.com/blog/1142358 (转载请注明出处) 用途说明 创建静 ...
- Java基础系列(16)- Scanner进阶使用
了解更多的sanner方法 Ctrl+鼠标左键,点击[Scanner] 点击Structure 看到了Scanner类下面的所有方法,以及具体方法实现的底层封装逻辑 拓展例子_nextInt()获取和 ...
- k8s garbage collector分析(2)-处理逻辑分析
garbage collector介绍 Kubernetes garbage collector即垃圾收集器,存在于kube-controller-manger中,它负责回收kubernetes中的资 ...
- sql case when 多条件小结
sql case when 多条件 小结 -- 第一种 格式 : 简单Case函数 : -- 格式说明 -- case 列名 -- when 条件值1 then 选择项1 -- when 条件值2 t ...
- python列表底层实现原理
Python 列表的数据结构是怎么样的? 书上说的是:列表实现可以是数组和链表.顺序表是怎么回事?顺序表一般是数组. 列表是一个线性的集合,它允许用户在任何位置插入.删除.访问和替换元素.列表实现是基 ...
- Hive——安装以及概述
一.hive的安装 注意:安装hive的前提要安装好MySQL和Hadoop Hadoop安装:https://www.cnblogs.com/lmandcc/p/15306163.html MySQ ...
- eclipse下载安装
1.下载 网址:https://www.eclipse.org/downloads/ 找到:eclipse-inst-jre-win64.exe 下载... 2.安装 找到-->Eclipse ...
- typora博客笔记上传图片时不能显示
前言 markdown具有轻量化.易读易写等特性,并且对于图片.超链接.图片.数学公式都有支持. 但是最近在使用Typora的过程中我发现,在写文章笔记的时候导入的图片,因为图片保存在我们电脑本地,当 ...
- vue3 element-plus 配置json快速生成table列表组件,提升生产力近500%(已在公司使用,持续优化中)
️本文为博客园首发文章,未获授权禁止转载 大家好,我是aehyok,一个住在深圳城市的佛系码农♀️,如果你喜欢我的文章,可以通过点赞帮我聚集灵力️. 个人github仓库地址: https:gith ...
- 【NOIP1998】 三连击 题解
文章转载前需和原作者联系,否则追究法律责任 题目链接:https://www.luogu.com.cn/problem/P1008 首先我们来分析一下题目.要求是枚举三个数,比例为1:2:3,且各个数 ...