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(双向动态规划)的更多相关文章

  1. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

    题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...

  2. 【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 ...

  3. 【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 ...

  4. 【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 ...

  5. HDU4870_Rating_双号从零单排_高斯消元求期望

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...

  6. 从零单排Linux – 3 – 目录结构

    从零单排Linux – 3 – 目录结构 1.FHS标准(filesystem hierarchy standard) why? –> 为了规范,还有为了linux的发展 重点 –> 规范 ...

  7. 从零单排Linux – 2 – 目录权限

    从零单排Linux – 2 – 目录权限 1.sync 讲内存数据跟新到硬盘中 2.执行等级init a: run level 0:关机 b: run level 3:纯命令模式 c:run leve ...

  8. 从零单排Linux – 1 – 简单命令

    从零单排Linux – 1 – 简单命令 Posted in: Linux 从零单排Linux – 1 一.Linux的简单命令: 1.忘记root密码: 读秒时按任意键进入 – e – ↓选择第二个 ...

  9. JAVA从零单排之前因

    本人,男,21岁,普通院校本科,计算机专业.大学之前对计算机编程没有一点涉及.大学学计算机专业也是个偶然.因为当初高考的成绩不好,结果都是我父亲帮我报的学校和专业. 上了大学之后,大一都是在新奇中度过 ...

随机推荐

  1. linux下創建啓動圖標

    Linux下如何为刚安装好的Eclipse在桌面建一个启动图标?(QtCreator 也可以类似去做). 首先:gedit    /usr/share/applications/eclipse.des ...

  2. linux中的strip命令简介------给文件脱衣服【转】

    转自:http://blog.csdn.net/stpeace/article/details/47090255 版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则 ...

  3. hadoop(二)hadoop的安装部署

    系统版本 : 64位CentOS6.6 hadoop版本: 1.2.1 jdk版本: jdk1.6.0_45 环境准备 1.主机分配 主机名 ip master 1.0.0.0.10 slave1 1 ...

  4. Cryptography I 学习笔记 --- 抗碰撞

    1. 生日攻击,如果hash函数可以产生n bit的结果,那么生日攻击的时间复杂度在O(nn/2)这个量级.以比特币使用的SHA256为例,其hash结果为256bit,那么如果想完成一次生日攻击,那 ...

  5. 10.1综合强化刷题 Day2

    a[问题描述]你是能看到第一题的 friends呢.                                                —— hja世界上没有什么比卖的这 贵弹丸三还令人绝 ...

  6. 你知道如何在springboot中使用redis吗

    特别说明:本文针对的是新版 spring boot 2.1.3,其 spring data 依赖为 spring-boot-starter-data-redis,且其默认连接池为 lettuce ​  ...

  7. flash+xml无法显示中文的解决办法

    flash+xml用来做图片动态浏览效果相当不错,被广泛运用于电子相册制作,很多朋友都会从网上下载一些相关的flash源码下载参考,但是经常发现在使用过程中,修改了xml文件中的英文后要么文本不显示, ...

  8. navicat链接lunix平台上的数据库

    xsell 4.navicat软件 想在链接数据库的得常规设置里设置: 链接名称.主机名(链接lunix平台后才干ping 通的ip地址) port.username.password 然后选择ssh ...

  9. 【GLSL教程】(九)其他说明 【转】

    http://blog.csdn.net/racehorse/article/details/6664775 法线矩阵 在很多顶点shader中都用到了gl_NormalMatrix.这里将介绍这个矩 ...

  10. Python代码优化概要

    Python即是面向过程语言,也是面向对象语言,很多其它情况下充当脚本语言的角色.虽是脚本语言,但相同涉及到代码优化的问题,代码优化可以让程序执行更快,它是在不改变程序执行结果的情况下使程序执行效率更 ...