[leet code 135]candy
1 题目
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?
2 思路
按照网上的思路,每个孩子至少有一个糖果,先从左到右遍历一遍,写出递增的糖果数,再从右到左遍历一遍完成递减的糖果数。这种大小与左右两边数据相关的问题,均可以采用这个思路。另外,有另一种空间复杂度O(1),时间复杂度O(n)的思路,可以参考http://www.cnblogs.com/felixfang/p/3620086.html。
3 代码
public int candy(int[] ratings) {
if(ratings == null || ratings.length == 0)
{
return 0;
}
int[] candyNums = new int[ratings.length];
candyNums[0] = 1; for(int i = 1; i < ratings.length; i++)
{
if(ratings[i] > ratings[i-1]) //如果第i个孩子比第i - 1孩子等级高,
{
candyNums[i] = candyNums[i-1]+1;
}
else //每人至少有一个糖果
{
candyNums[i] = 1;
}
} for(int i = ratings.length-2; i >= 0; i--)
{ if(ratings[i] > ratings[i + 1] && candyNums[i] <= candyNums[ i + 1]) //如果第i个孩子比第i + 1孩子等级高并且糖果比i+1糖果少
{
candyNums[i] = candyNums[i + 1] + 1;
}
} int total = 0;
for (int i = 0; i < candyNums.length; i++) {
total += candyNums[i];
} return total;
}
[leet code 135]candy的更多相关文章
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- 【Leet Code】Palindrome Number
Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...
- Leet Code 771.宝石与石头
Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S ...
- 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 ...
- 【LeetCode】135. Candy
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 135. Candy(Array; Greedy)
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
随机推荐
- PC 上的 LVM 灾难修复
LVM 介绍 LVM 简介 LVM 是逻辑盘卷管理(Logical Volume Manager)的简称,最早是 IBM 为 AIX 研发的存储管理机制.LVM 通过在硬盘和分区之间建立一个逻辑层,可 ...
- UI设计:C4D作品案例分享
中文名4D电影,外文名CINEMA 4D,研发公司为德国Maxon Computer,特点为极高的运算速度和强大的渲染插件,使用在电影<毁灭战士>.<阿凡达>中,获得贸易展中最 ...
- [Robot Framework] 搭建Robot Framework和RIDE(Robot Framework GUI) 的环境
在windows x64的环境上进行安装,集成Selenium2和AutoIt的libraries,以下安装步骤在win 7,win 8.1,win 10, win 2012 R2上测试通过 1. 下 ...
- mysql8.0.4以后修改密码方式变更
https://blog.csdn.net/qq_38265784/article/details/80915098 use mysql: ALTER USER 'root'@'localhost' ...
- ExportGrid Aspose.Cells.dll
using Aspose.Cells; using Aspose.Words; using System; using System.Collections; using System.Collect ...
- Permutations LT46
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- xcode如何运行下载的demo工程
1. 首先你需要改Bundle Identifier,修改成别人没注册过的. 2. 选中 “Automatically manage signing” 3. 在Team里面添加自己的个人帐号了.
- Android NDK定位.so文件crash代码位置
参考:http://blog.csdn.net/xyang81/article/details/42319789 问题: QRD8926_110202平台的Browser必现报错.(去年的项 ...
- mybatis 的查询某个字段的特定位数(模糊查询)
获取特定的几位:1.取url字段后三位字符 select SUBSTRING(url, -3) from link; 2.取url字段前三位字符 select SUBSTRING(url, 3) fr ...
- spring学习 六 spring与mybatis整合
在mybatis学习中有两种配置文件 :全局配置文件,映射配置文件.mybatis和spring整合,其实就是把mybatis中的全局配置文件的配置内容都变成一个spring容器的一个bean,让sp ...