【LeetCode】135. Candy
Candy
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?
左右各遍历一次,依据前一个位置的candy数计算当前位置需要的最小candy数(最少为1)
由于两次遍历的结果都要满足,因此对同一位置取max即可。
class Solution {
public:
int candy(vector<int>& ratings) {
if(ratings.empty())
return ;
int n = ratings.size();
vector<int> left(n, );
for(int i = ; i < n; i ++)
{
if(ratings[i] > ratings[i-])
left[i] = left[i-] + ;
}
vector<int> right(n, );
int sum = max(left[n-], right[n-]);
for(int i = n-; i >= ; i --)
{
if(ratings[i] > ratings[i+])
right[i] = right[i+] + ;
sum += max(left[i], right[i]);
}
return sum;
}
};
【LeetCode】135. Candy的更多相关文章
- 【LeetCode】723. Candy Crush 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...
- 【leet-code】135. 加油站
题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其 ...
- 【LeetCode】数组--合并区间(56)
写在前面 老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- html调用servlet(JDBC在Servlet中的使用)(2)
5.修改数据 5.1编写查询条件页面 修改单条数据的时候,首先是查询出单个数据的详细信息,然后根据实际需要部分修改或者全部修改.修改之后,数据会提交到数据库,数据库中保存更新以后的数据. 查询出单条数 ...
- 混沌数学之Kent模型
相关软件:混沌数学之离散点集图形DEMO 相关代码: // http://wenku.baidu.com/view/7c6f4a000740be1e650e9a75.html // 肯特映射 clas ...
- 第二十一章 springboot + 定时任务
1.application.properties #cron job.everysecond.cron=0/1 * * * * * job.everytensecond.cron=0/10 * * * ...
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- 手机WiFi万能钥匙查看破解的password和手机查询命令收集
手机须要网络利用WiFi万能钥匙破解了WIFI的password.手机就能够上网了,但假设想在电脑上使用手机破解的Wifi热点上网就须要password,此时须要知道手机破解的password,WiF ...
- 解决百度ueditor支持iframe框架页面的视频播放问题
新下载的ueditor 增加了xss 安全过虑,把iframe过滤了,导致发表的文章包含的视频播放功能被限制了. 说明:新版本ueditor要修改 xss过滤白名单 修改配置文件ueditor.con ...
- VS2008+Windows DDK 7的环境配置(二)
在第一篇的基础上,进行如下的步骤,就可以编译出X64的驱动程序. (建议再另外建一个项目,这样避免混淆,因为x86和x64编译的有些编译选项是不同的.) 1. 安装VS2008 x64 build 组 ...
- Winform控件之DataGridView数据控件显示问题
近期在做同类的信息记录管理系统时遇到了DataGridView数据控件的显示问题.可能是2015年的上半年没有深入 学习C#开发的原因.这几天又一次搬出来开发,首先遇到的问题就是动态绑定数据显示的问题 ...
- objective-c 字符串基本操作
.定义一个字符串a, 截取a 的某一个部分,复制给b, b必须是int型 NSString *a = @"1.2.30"; ,)] intValue]; NSLog(@" ...
- android 随手记 读写文件的几种方式
java中多种方式读文件 一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内容 4.随机读取文件内容 */ import java.io.BufferedRe ...