【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-十大经典排序算法
随机推荐
- UI_UITabBarController
建立控制器 // 普通控制器 GroupViewController *groupVC = [[GroupViewController alloc] init]; SecondViewControll ...
- 加载依赖的jar包在命令行编译和运行java文件
在命令里编译和执行java文件,当应用程序需要需要依赖的jar包里面的class文件才能编译运行的时候,应该这样做: 1. 首先是编译过程,在命令行里面执行: (1) javac -classpath ...
- Ubuntu 12.04 安装配置 Apache2
Apache2安装 1 我们使用root账户进行安装,首先切换到root账户,输入命令: sudo su 2 安装 Apache2 apt-get install apache2 在浏览器输入你服务器 ...
- Combinations leetcode java
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- ASP.NET MVC 编程参考
ASP.NET MVC 编程参考 转载请注明出处:http://surfsky.cnblogs.com MVC 参考 http://msdn.microsoft.com/zh-cn/dd40 ...
- statickeyword
static词义:静态的,可以用于修饰变量和方法,static方法块可以优先于构造函数运行. 被static修饰的变量,叫静态变量,静态变量在内存中仅仅有一份拷贝 public static Stri ...
- LA 4728 Square ,旋转卡壳法求多边形的直径
给出一些正方形.让你求这些正方形顶点之间的最大距离的平方. //返回点集直径的平方 int diameter2(vector<Point> & points) { vector&l ...
- GO语言基础之interface
接口interface 1. 接口是一个或多个方法签名的集合 2. 只要某个类型拥有该接口的所有方法签名,即算实现该接口,无需显示声明实现了哪个接口,这称为 Structural typing 3. ...
- SMTP 协议系列一
解说一下DOS下telnet命令发送邮件 步骤,以我的163邮箱为例 1.開始-->cmd 进入到dos里面 2.输入telnet smtp.163.com 25 C: \Users \Ad ...
- ubuntu apache2 虚拟主机服务
ubuntu apache2 虚拟主机服务 本次配置的是一个 ip 对应多个 虚拟主机 1:先检查 ubuntu server 是否已经安装了 apache2 web服务: apache2 -v 看到 ...