[LeetCode OJ] 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?
class Solution {
public:
int candy(vector<int> &ratings) {
vector<int> candy_number(ratings.size(), );
//不用迭代器,改用下标索引实现,这样代码看起来更加简洁
for(int i=; i<ratings.size(); ++i)
{
//保障当右边的rating比左边的高时,则右边分得的糖果比左边多
if( ratings[i] > ratings[i-]) //eg:输入[4 2 3 4 1],最少的分法是[2 1 2 3 1],分发9个糖果
candy_number[i] = (candy_number[i] > candy_number[i-]+) ? candy_number[i] : candy_number[i-]+;
} for(int i=ratings.size()-; i>=; --i)
{
//保障当左边的rating比右边的高时,则左边分得的糖果比右边多
if( ratings[i] > ratings[i+])
candy_number[i] = (candy_number[i] > candy_number[i+]+) ? candy_number[i] : candy_number[i+]+;
} int total=;
for(int i=; i!=ratings.size(); ++i)
total = total + candy_number[i]; return total;
}
};
[LeetCode OJ] Candy的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- LeetCode OJ 之 Maximal Square (最大的正方形)
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- LeetCode OJ:Integer to Roman(转换整数到罗马字符)
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- mysql 安装补充
1:假如下载的文件名为:mysql-5.0.45.tar.gz 2:假如copy到 /usr/local下 3:groupadd mysql #添加mysql组 4:useradd -g mysql ...
- 常用的用户状态命令包括:whoami、id、groups、newgrp 等
用户状态命令 常用的用户状态命令包括:whoami.id.groups.newgrp 等.
- ural-1099-Work Scheduling(裸带花树)
题意: 有N个人,有限对的人可以在一起工作,问最多能有多少对. 分析: 任意图的最大匹配 // File MAXName: 1099.cpp // Author: Zlbing // Created ...
- (转载)php 合并数组中的数据,如果键值相等其值相加
(转载)http://hi.baidu.com/syxrrrr/item/7dfb2387343ce0874414cfdd /* * 合并数据,如果键值相等其值相加 * @param array $d ...
- 【图论】Codeforces 711D Directed Roads
题目链接: http://codeforces.com/problemset/problem/711/D 题目大意: 给一张N个点N条有向边的图,边可以逆向.问任意逆向若干条边使得这张图无环的方案数( ...
- 【动态规划】【最短路】Codeforces 710E Generate a String
题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...
- [tools] sublime 使用记录
1. 目录下的文本搜索功能(自带) 1). 把文件夹拖到 sublime 上 2). 在 sublime 上展开要搜索的目录,右击,选择[find in folder] 2. sublime cons ...
- hdu 4579 博弈+区间dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 #include <cstdio> #include <cstring> ...
- Intellij 部署maven项目
一 部署Maven项目 1.下载和配置 (1)下载:maven.apache.org,点击download,下载apache-maven-3.3.9-bin.zip (2)配置环境变量: 环境变量包括 ...
- 发起SSH攻击主机IP地址列表
发起SSH攻击主机IP地址列表 东北大学 http://antivirus.neu.edu.cn/scan/ssh.php 以下IP地址对SSH服务进行攻击,严重增加主机成为肉鸡的可能性.强烈建议网管 ...