【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岁,普通院校本科,计算机专业.大学之前对计算机编程没有一点涉及.大学学计算机专业也是个偶然.因为当初高考的成绩不好,结果都是我父亲帮我报的学校和专业. 上了大学之后,大一都是在新奇中度过 ...
随机推荐
- 实现多线程sokect
上一篇文章说到怎样写一个最简单的Java Socket通信,但是在上一篇文章中的例子有一个问题就是Server只能接受一个Client请求,当第一个Client连接后就占据了这个位置,后 续Clien ...
- duilib入门简明教程 -- 第一个程序 Hello World(3) (转)
原文转自 http://www.cnblogs.com/Alberl/p/3343579.html 小伙伴们有点迫不及待了么,来看一看Hello World吧: 新建一个空的win32项目,新建一个m ...
- (4)JavaScript引用类型
Object类 创建object实例的方式有两种 1.第一种是使用 new 操作符后跟 Object 构造函数 var person = new Object(); person.name = &qu ...
- 在Linux 双机下自己手动实现浮动ip技术
两台Linux服务器,一台为主机(IP:124.158.26.30)对外提供了一定的网络服务,另一台从机(IP:124.158.26.31)能提供相同的服务,但ip地址没有对外部公开客户端连接的都是1 ...
- oracle中的替换函数replace和translate函数
.translate 语法:TRANSLATE(char, from, to) 用法:返回将出现在from中的每个字符替换为to中的相应字符以后的字符串. 若from比to字符串长,那么在from中比 ...
- Linux文件名命名规范
注意:在Linux下全部文件和文件夹命名都是小写的! Linux系统区分英文字符的大小写.命名目录和命名文件的规则是相同的.除非有特别的原因,否则用户创建的文件和目录名要使用小写字符.大多数的Linu ...
- Jenkins内存溢出的处理方法
参考:http://openwares.net/java/jenkens_deploy_to_tomcat_error_of_outofmemoryerror.html上的说明,有如下解释: -Xms ...
- Scut游戏服务器引擎之Unity3d接入
Scut提供Unity3d Sdk包,方便开发人员快速与Scut游戏服务器对接: 先看Unity3d示例如下: 启动Unity3d项目 打开Scutc.svn\SDK\Unity3d\Assets目录 ...
- 鼠标悬浮弹出标题制作JQuery
今天给客户制作的网站里面加个效果,当鼠标在列表图片之外时,标题不显示,当鼠标悬浮在图片之上时,标题从底部弹出. 效果图如下: 鼠标悬浮前: 鼠标悬浮后: html代码如下: <ul class= ...
- Ubuntu下sudo apt-get install vim 失败的解决办法
Ubuntu下 执行命令:sudo apt-get install vim 失败 解决办法: 更新一下,命令:sudo apt-get update 再安装即可成功:sudo apt-get inst ...