135. Candy(Array; Greedy)
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?
思路:neighbor=>元素值与前、后元素有关=>
第一次前向扫描:保证high rate元素比左邻居糖多;
第二次后向扫描:保证high rate元素比右邻居糖多;
class Solution {
public:
int candy(vector<int> &ratings) {
vector<int> new_ratings(ratings.size(), );
for(int i = ; i < ratings.size(); i++) //for the first time, scan from beginning
{
if(ratings[i] > ratings[i-])
{
new_ratings[i] = new_ratings[i-]+;
}
}
for(int i=ratings.size()-;i>=;i--){ //for the second time, scan from the end
if(ratings[i]>ratings[i+]&&new_ratings[i]<=new_ratings[i+]){
new_ratings[i]=new_ratings[i+]+;
}
}
int sum=;
for(int i=;i<new_ratings.size();i++){
sum+=new_ratings[i];
}
return sum;
}
};
135. Candy(Array; Greedy)的更多相关文章
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- leetcode 135. Candy ----- java
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- Leetcode#135 Candy
原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
- 135. Candy
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
- [leet code 135]candy
1 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 55. Jump Game (Array; Greedy)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode】135. Candy
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- Java for LeetCode 135 Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
随机推荐
- 对两个奇葩的C语言程序的思考
原文章的连接为:http://www.cnblogs.com/jacksu-tencent/default.html?page=2 1. 第一个程序例如以下: #include <stdio.h ...
- odoo10 修改产品单价的小数点位数
odoo10 修改产品单价的小数点位数 由于产品价格原因,单价需要保留小数点后 5 位,所以需要修改单价的小数点位数. 开启开发模式 找到数据库编辑 找到小数点精度 修改产品的小数点位数
- C# WinForm页面切换导致闪烁的解决方法(转)
问题描述 界面上放置大量的控件(尤其是自定义控件)会导致在窗体加载时,速度变得缓慢:当切换页面时,也会时常产生闪烁的问题,非常影响用户体验. 解决方法 将此代码写在要解决闪烁问题的父窗体中: prot ...
- 使用Maven运行Solr(翻译)
Solr是一个使用开源的搜索服务器,它采用Lucene Core的索引和搜索功能构建,它可以用于几乎所有的编程语言实现可扩展的搜索引擎. Solr的虽然有很多优点,建立开发环境是不是其中之一.此博客条 ...
- ubuntu :安装一个方便的终端工具,Guake Terminal
前言: 刚刚用ubuntu不久,就想找点新鲜玩意儿,提高linux能力,当然啦,我喜欢折腾,今天就碰到Guake Terminal这个玩意儿,它的功能很简单,就是一个终端,按F12就出现,再按就隐藏, ...
- STL容器——对map排序
STL容器(三)——对map排序 对于map的排序问题,主要分为两部分:根据key排序:根据value排序.下面我们就分别说一下~ 1. 根据key进行排序 map默认按照key进行升序排序 ,和输入 ...
- Eclipse中添加web dynamic project【菜鸟学JAVA】
很多eclipse版本是不能直接新建web dynamic project的,需要从网上找插件或更新.我的Eclipse的版本是(Version: 3.7.0) 比较方便的是在Help → Insta ...
- Win10 TensorFlow(gpu)安装详解
Win10 TensorFlow(gpu)安装详解 写在前面:TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理.Tensor(张量)意味着 ...
- 一行代码搞定 FTP 服务
环境搭建: python windows/linux pip install pyftpdlib (安装失败请到这里下载:https://pypi.python.org/pypi/pyftpdlib/ ...
- user
test|123456 raolan|123456 test2|456123