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 ...
随机推荐
- PTA——c++2017Final 圆周率山
为了参加学校的社团风采展,怡山小学数学组的同学们决定画一座圆周率山,以宣传圆周率. 已知圆周率为:3. 1415926535 8979323846 2643383279 5028841971 6939 ...
- Xftp乱码问题
Xftp出现乱码 修改编码
- Appium 自动化测试改造思路
流水账脚本 从头到尾编写测试脚本 PO封装 业务行为与操作具体页面元素分离 basepage封装 如封装find方法,目的时增强稳定性 数据驱动封装 将常用的数据改为配置文件 为构建测试平台打基础
- 启动jemeter 报错相关解决方案
1:当启动jemeter时报错"页面文件太小,无法完成操作" 如图: 是说明分配的内容不足,即可调整内存重启即可解决 1):打开:控制面板>系统和安全>系统 2):点击 ...
- Navicat连接数据库成功,新建查询时提示错误“Cannot create file ……”
Navicat连接数据库成功,新建查询时提示错误"Cannot create file --" 原因:编辑连接{高级}<设置位置>被修改,该oci.dll不正确 解决方 ...
- CF932G-Palindrome Partition【PAM】
正题 题目链接:https://www.luogu.com.cn/problem/CF932G 题目大意 给出一个长度为\(n\)的字符串,将其分为\(k\)段(\(k\)为任意偶数),记为\(p\) ...
- 关于Postman你必须学会的技能
关于Postman 工欲善其事,必先利其器,在了解了接口测试之后,就要选择一款适用的工具.之所以选择postman是因为它简单.容易上手.能覆盖大多数HTTP接口测试场景,性价比极高. Postman ...
- vue成就购物城的功能 (展示增删改查)
<!DOCTYPE html><html> <!DOCTYPE html> <html> <head> <meta charset=& ...
- 测试rac数据文件建本地及处理
模拟用户zytuser的表空间ZYTUSER_TBS表空间添加数据文件到本地.--环境准备1.创建一个表空间--创建表空间create tablespace ZYTUSER_TBS datafile ...
- Geocoding Tools(地理编码工具)
地理编码工具 # Process: 创建地址定位器 arcpy.CreateAddressLocator_geocoding("", "", "&qu ...