leetcode 135. Candy ----- java
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、找出从小到大排列的子序列,大小为n,分配的糖果应该是1、2、3....n
2、如果 n == 1 那么就应该是比之前子序列的开头要大,即 : 如果之前子序列中数字个数大于1,那么该位置分配2个糖果(应该之后一个位置的孩子得到了1个糖果),否则得到比后一个位置的孩子糖果多1 的糖果。
3、比较麻烦的是如果遇到相同rating的孩子:相同rating 的孩子可以得到不一样的糖果。
因此每次结束的时候需要判断当前子序列结尾的数字是否与下一个子序列开始的位置的数字相同,如果不相同,需要当前子序列结尾得到的数目大于在一个子序列开始的数目;如果一样,那么可以按照之前的顺序分配,即1、2、3、4...n
public class Solution {
public int candy(int[] ratings) {
int len = ratings.length;
int result = 0;
int pos = len-1;
int flag = 0,target = 0;
while( pos >= 0){
int size = 1;
while( pos >= 1 && ratings[pos-1] < ratings[pos] ){
size++;
pos--;
target = ratings[pos];
}
if( size > 1){
result+=(1+size)*size/2;
if(size <= flag ){
if( pos+size<len && ratings[pos+size] != ratings[pos-1+size])
result+=(flag-size+1);
}
flag = 1;
}
else {
if( pos<len-1 && ratings[pos] == ratings[pos+1]){
flag = 1;
result+=1;
}
else{
result += flag+1;
flag++;
}
}
pos--;
}
return result;
}
}
leetcode 135. Candy ----- java的更多相关文章
- LeetCode 135 Candy(贪心算法)
135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...
- Java for LeetCode 135 Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重
原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...
- Leetcode#135 Candy
原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...
- [leetcode] 135. Candy (hard)
原题 前后两遍遍历 class Solution { public: int candy(vector<int> &ratings) { vector<int> res ...
- Java实现 LeetCode 135 分发糖果
135. 分发糖果 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果. ...
- [Leetcode 135]糖果分配 Candy
[题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【LeetCode】135. Candy
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 135. Candy
题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...
随机推荐
- 【STL】-priority_queue的用法
初始化: priority_queue<int> maxPQ; priority_queue<int,vector<int& ...
- exit(0)与exit(1)、return区别
exit(0):正常运行程序并退出程序: exit(1):非正常运行导致退出程序: return():返回函数,若在主函数中,则会退出函数并返回一值. 详细说: 1. return返回函数值,是关键字 ...
- Android模拟器配置选项说明
Memory Options是模拟器的运行内存大小,类比电脑内存大小,就是在设置->应用程序中,正在运行标签页下面显示的那个大小Internal storage是模拟器内置存储空间大小,用于存放 ...
- 安装xampp后,遇到的各种问题
一.apache无法启动 1.查看端口是否被占用 80端口冲突,解决方法:打开目录C:\xampp\apache\conf(我的安装目录为C:\xampp)下的httpd.conf文件,将Listen ...
- 获取hadoop的源码和通过eclipse关联hadoop的源码
一.获取hadoop的源码 首先通过官网下载hadoop-2.5.2-src.tar.gz的软件包,下载好之后解压发现出现了一些错误,无法解压缩, 因此有部分源码我们无法解压 ,因此在这里我讲述一下如 ...
- libimobiledevice安装步骤
https://github.com/libimobiledevice/libimobiledevice libimobiledevice安装指南,你还不知道libimobiledevice为何物,赶 ...
- Security Checklist (路由器安全checklist)
Security Checklist Website by Michael Horowitz Home | Introduction | Router Bugs | Security Che ...
- php生成图片注释
//生成验证码图片注释 <?php session_start(); $arr = array( 'a','b','c','d','e','f','g','h','i','j','k','l', ...
- bat批处理文件命令详解
bat批处理文件命令详解 echo.@.call.pause.rem(小技巧:用::代替rem)是批处理文件最常用的几个命令 echo 表示显示此命令后的字符 echo off 表示在此语句后所有运 ...
- XML HttpRequest
XMLHttpRequest对象提供了在网页加载后与服务器进行通信的方法. 使用XMLHttpRequest对象,能够: 在不重新加载页面的情况下更新网页 在页面已加载后从服务器请求数据,接受数据 在 ...