LeetCode135: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?
解题思路:
遍历两遍数组即可
第一遍:如果当前元素比前一个大,则当前小孩获得的糖果数为前一个小孩糖果数+1,否则糖果数为1
第二遍:从后往前扫描,如果当前元素i的值大于i+1位置的值,则比较两者目前的糖果数,如果i小孩获得的糖果数大于第i+1个小孩获得糖果数+1,则不变,否则,将i小孩糖果数置为第i+1个小孩糖果数+1.
实现代码:
#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
int candy(vector<int> &ratings) {
int len = ratings.size();
if(len == || len == )
return len;
int *c = new int[len];
c[] = ;
for(int i = ; i < len; i++)
if(ratings[i] > ratings[i-])
c[i] = c[i-] + ;
else
c[i] = ;
int minCandy = c[len-];
for(int i = len-; i >= ; i--)
{
if(ratings[i] > ratings[i+])
c[i] = max(c[i], c[i+] + );
minCandy += c[i];
}
return minCandy; }
}; int main(void)
{
int ratings[] = {,,,,,,};
int len = sizeof(ratings) / sizeof(ratings[]);
vector<int> ratVec(ratings, ratings+len);
Solution solution;
int ret = solution.candy(ratVec);
cout<<ret<<endl;
return ;
}
LeetCode135:Candy的更多相关文章
- lc面试准备:Candy
1 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- LeetCode OJ:Candy(糖果问题)
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- [LeetCode 题解]:Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- 九度OJ 1145:Candy Sharing Game(分享蜡烛游戏) (模拟)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:248 解决:194 题目描述: A number of students sit in a circle facing their teac ...
- 转载:Candy? 在线性时间内求出素数与欧拉函数
转载自:http://www.cnblogs.com/candy99/p/6200660.html 2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB ...
- 【树状数组(二叉索引树)】轻院热身—candy、NYOJ-116士兵杀敌(二)
[概念] 转载连接:树状数组 讲的挺好. 这两题非常的相似,查询区间的累加和.更新结点.Add(x,d) 与 Query(L,R) 的操作 [题目链接:candy] 唉,也是现在才发现这题用了这个知识 ...
- 【原创】leetCodeOj --- Candy 解题报告
题目地址: https://leetcode.com/problems/candy/ 题目内容: Candy Total Accepted: 43150 Total Submissions: 2038 ...
- 水题:HDU1034-Candy Sharing Game
解题心得: 1.我是用的模拟的算法直接模拟的每一轮的分配方法的得出的答案,看到有些大神使用的链表做的,好像链表才是这道题的镇长的做法吧. 题目: Candy Sharing Game Time Lim ...
- 模拟:HDU1034-Candy Sharing Game
解题心得: 1.直接模拟每一次分一半就行了,模拟过程,记录轮数,但是也看到有些大神使用的是链表,估计链表才是真的做法吧. 题目: Candy Sharing Game Time Limit: 2000 ...
随机推荐
- httpclient4例子
参考:http://hc.apache.org/httpclient-3.x/tutorial.html import org.apache.http.HttpEntity; import org.a ...
- 求2的n次方对1e9+7的模,n大约为10的100000次方(费马小定理)
昨天做了一个题,简化题意后就是求2的n次方对1e9+7的模,其中1<=n<=10100000.这个就算用快速幂加大数也会超时,查了之后才知道这类题是对费马小定理的考察. 费马小定理:假如p ...
- Excel Sheet Column Number(STRING-TYPE CONVERTION)
QUESTION Related to question Excel Sheet Column Title Given a column title as appear in an Excel she ...
- [SCOI2007]修车(建图好题)
[SCOI2007]修车 https://www.lydsy.com/JudgeOnline/problem.php?id=1070 Time Limit: 1 Sec Memory Limit: ...
- iOS 网络请求中的空类型字符串转换
创建一个工具类, .h: #import <Foundation/Foundation.h> @interface MySetNullWithStrTool : NSObject +( ...
- 【校招面试 之 剑指offer】第9-1题 用两个栈实现一个队列
#include<iostream> #include<stack> using namespace std; template <typename T> void ...
- [leetcode]392. Is Subsequence 验证子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
- Legendre多项式
Legendre多项式 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Legendre多项式的递归公式
- c++ 自动对象
转自: https://www.cnblogs.com/geloutingyu/p/8034904.html 1.自动对象默认情况下,局部变量的生命期局限于所在函数的每次执行期间.只有当定义它的函数被 ...
- Windows系统文件mshtml.dll
今天,在vista 32bit,sp 2,IE7的机器上跑开发的软件产品,打开IE,被测系统总是崩溃,换了一台机器,同样的配置环境,却没有重现. 同事的分析很详细,学习了 I tried this c ...