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 ...
随机推荐
- [Apache Atlas] Atlas 架构设计及源代码简单分析
Apache Atlas 架构图 Atlas 支持多数据源接入:Hive.HBase.Storm等 Type System Type Atlas 中定义了一些元数据类型 ── AtlasBaseTyp ...
- 截断误差VS舍入误差
截断误差:是指计算某个算式时没有精确的计算结果,如积分计算,无穷级数计算等,使用极限的形式表达的,显然我们只能截取有限项进行计算,此时必定会有误差存在,这就是截断误差. 舍入误差:是指由于计算机表示 ...
- CodeForce-782B The Meeting Place Cannot Be Changed(高精度二分)
https://vjudge.net/problem/CodeForces-782B B. The Meeting Place Cannot Be Changed time limit per tes ...
- 解决使用tomcat服务器发布web项目时出现URL中文乱码的问题
打开Tomcat的安装路径 打开server.xml文件 在修改端口号的一行既是下图中位置添加 URIEncoding="UTF-8" 就能替换在用eclipse或者myeclip ...
- TP5增加扩展配置目录
ThinkPHP5.0.1版本开始增加了扩展配置目录的概念,在应用配置目录或者模块配置目录下面增加extra子目录,下面的配置文件都会自动加载,无需任何配置. 这极大的方便了我们进行扩展配置,比如在a ...
- selenium下拉选择框处理
HTML: (一)通过xpath层级标签定位 driver.find_element_by_xpath(".//*[@id='Resolution']/option[2]").cl ...
- [gdoi2018 day1]小学生图论题【分治NTT】
正题 题目大意 一张随机的\(n\)个点的竞赛图,给出它的\(m\)条相互无交简单路径,求这张竞赛图的期望强联通分量个数. \(1\leq n,m\leq 10^5\) 解题思路 先考虑\(m=0\) ...
- YbtOJ#752-最优分组【笛卡尔树,线段树】
正题 题目链接:http://www.ybtoj.com.cn/problem/752 题目大意 \(n\)个人,每个人有\(c_i\)和\(d_i\)分别表示这个人所在的队伍的最少/最多人数. 然后 ...
- Servlet和Servlet容器
Java Servlet(Java服务器小程序)是一个基于Java技术的Web组件,运行在服务器端,它由Servlet容器所管理,用于生成动态的内容, Servlet是平台独立的Java类,编写一个S ...
- 【集成学习】:Stacking原理以及Python代码实现
Stacking集成学习在各类机器学习竞赛当中得到了广泛的应用,尤其是在结构化的机器学习竞赛当中表现非常好.今天我们就来介绍下stacking这个在机器学习模型融合当中的大杀器的原理.并在博文的后面附 ...