【LeetCode从零单排】No.135Candy(双向动态规划)
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.代码
public class Solution {
   	public int candy(int[] ratings){
		int size=ratings.length;
		if(size<=1) return size;
		int[] nums=new int[size];
		for(int i=0;i<size;i++){
             nums[i]=1;
		}
		for(int j=1;j<size;j++){
			if(ratings[j]>ratings[j-1]) nums[j]=nums[j-1]+1;
		}
		for(int m=size-1;m>0;m--){
			if(ratings[m-1]>ratings[m]){
				nums[m-1]=Math.max(nums[m]+1,nums[m-1]);
			}
		}
		int result=0;
		for(int n=0;n<size;n++){
			result+=nums[n];
		}
		return result;
   	}
}
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
【LeetCode从零单排】No.135Candy(双向动态规划)的更多相关文章
- 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters
		
题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...
 - 【LeetCode从零单排】No189	.Rotate Array
		
称号 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...
 - 【LeetCode从零单排】No15	 3Sum
		
称号 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
 - 【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List
		
题目 Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 ...
 - HDU4870_Rating_双号从零单排_高斯消元求期望
		
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...
 - 从零单排Linux – 3 – 目录结构
		
从零单排Linux – 3 – 目录结构 1.FHS标准(filesystem hierarchy standard) why? –> 为了规范,还有为了linux的发展 重点 –> 规范 ...
 - 从零单排Linux – 2 – 目录权限
		
从零单排Linux – 2 – 目录权限 1.sync 讲内存数据跟新到硬盘中 2.执行等级init a: run level 0:关机 b: run level 3:纯命令模式 c:run leve ...
 - 从零单排Linux – 1 – 简单命令
		
从零单排Linux – 1 – 简单命令 Posted in: Linux 从零单排Linux – 1 一.Linux的简单命令: 1.忘记root密码: 读秒时按任意键进入 – e – ↓选择第二个 ...
 - JAVA从零单排之前因
		
本人,男,21岁,普通院校本科,计算机专业.大学之前对计算机编程没有一点涉及.大学学计算机专业也是个偶然.因为当初高考的成绩不好,结果都是我父亲帮我报的学校和专业. 上了大学之后,大一都是在新奇中度过 ...
 
随机推荐
- Sql Server 2005 mdf、ldf文件无法复制问题
			
[问题原因]Sql Server服务只要启动就一直占用,故无法进行编辑操作. [解决办法 - 1]: 1)在开始-运行对话框中输入"services.msc”,显示如下界面: 2)关闭如上选 ...
 - C#生成高清缩略图的方法
			
/// <summary> /// 为图片生成缩略图 /// </summary> /// <param name="phyPath">原图片的 ...
 - web项目中引入jquery easyui
			
jQuery easyui是一个基于jquery的用户界面插件集合,可以做出各种炫酷页面效果,大中型项目都可以使用些框架,非常好用,而且它有中文网,提供了大量的demo.下面我们看怎么将它引入到项目中 ...
 - 三、Ubuntu 使用Xshell
			
1.下载一个Xshell 2.输入命令:ssh root@Ubuntu的ip地址 输入密码,即可连接 如果遇到不可连接.在Ubuntu中输入命令 vim /etc/ssh/sshd_config 更 ...
 - android日期时间选择器
			
android原生的日期时间控件,因为是原生的总有其满足不了我们需求的时候,Android 手机版本那么多,用户弹出来的控件五花八门.因为项目需要,在网上找了一 些demo看了看,感觉有些写的很好,很 ...
 - ReactiveCocoa 迎接下一个更加美好的世界
			
什么是ReactiveCocoa 如果你有看Github的Trending Objective-C榜单,那你肯定是见过ReactiveCocoa了.如果你在weibo上关注唐巧.onevcat等国内一 ...
 - linux yum 安装软件
			
概括了部分常用的yum命令包括: 自动搜索最快镜像插件:yum install yum-fastestmirror安装yum图形窗口插件:yum install yumex 1 安装yum insta ...
 - ArcGIS教程:公布地理处理服务
			
要公布地理处理服务.您须要两个元素:结果 窗体中的结果和到 ArcGIS Server 的管理员或公布者连接. 要公布服务,请右键单击结果并选择共享为 > 地理处理服务.例如以下图所看到的.此操 ...
 - Xcode中的变量模板(variable template)的使用方法
			
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! hopy ;) 你可能常常会写一些小的代码片段,里面自然少不了一些关键的变量. ...
 - 一个免费的API-手机号码归属地接口
			
手机号码归属地接口:根据手机号码或手机号码的前7位,查询手机号码归属地信息,包括省份 .城市.区号.邮编.运营商和卡类型. 接口文档:https://www.juhe.cn/docs/api/id/1 ...