题目

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?

题解

这道题和Trapping water那个是一样的想法,因为无论是水坑还是得到糖的小朋友,影响因素都不只一边,都是左右两边的最小值/最大值来决定的。

所以这道题跟上一道一样,也是左右两边遍历数组。

leftnums数组存从左边遍历,当前小朋友对比其左边小朋友,他能拿到糖的数量;

rightnums数组存从右边遍历,当前小朋友对比其右边小朋友,他能拿到的糖的数量。

最后针对这两个数组,每个小朋友能拿到的糖的数量就是这两个数最大的那个数,求总加和就好了。

代码如下:

 1     public int candy(int[] ratings) {  
 2         if(ratings==null || ratings.length==0)
 3             return 0;  
 4           
 5         int[] leftnums = new int[ratings.length];  
 6         int[] rightnums = new int[ratings.length];
 7         
 8         leftnums[0]=1;  
 9         for(int i=1;i<ratings.length;i++){  
             if(ratings[i]>ratings[i-1])  
                 leftnums[i] = leftnums[i-1]+1;  
             else  
                 leftnums[i] = 1;  
         }
         
         rightnums[ratings.length-1] = leftnums[ratings.length-1];  
         for(int i=ratings.length-2;i>=0;i--){
             if(ratings[i]>ratings[i+1]) 
                 rightnums[i] = rightnums[i+1]+1;
             else
                 rightnums[i] = 1;
                 
         }
         
         int res = 0;
         for(int i = 0; i<ratings.length; i++)
             res += Math.max(leftnums[i],rightnums[i]);
         
         return res;  
     } 

Candy leetcode java的更多相关文章

  1. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  2. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  3. Regular Expression Matching leetcode java

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  4. Sqrt(int x) leetcode java

    Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735  题目: Implement int sqrt(int x). Co ...

  5. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  6. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. *candy——leetcode

    /* */ #include<iostream> #include<vector> //#include<algorithm> #include <windo ...

  8. 【目录】LeetCode Java实现

    这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...

  9. Single Number II leetcode java

    问题描述: Given an array of integers, every element appears three times except for one. Find that single ...

随机推荐

  1. Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之flanneld网络介绍及部署(三)

    0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.flanneld介绍 ...

  2. BZOJ 4003: [JLOI2015]城池攻占 左偏树 可并堆

    https://www.lydsy.com/JudgeOnline/problem.php?id=4003 感觉就是……普通的堆啊(暴论),因为这个堆是通过递归往右堆里加一个新堆或者新节点的,所以要始 ...

  3. MVC高级编程-目录

    MVC高级编程 ================================================== 控制器 视图 模型 表单和HTML辅助方法 数据注解和验证 成员资格.授权和安全性 ...

  4. IO流-递归删除带内容的目录

    /* * 需求:递归删除带内容的目录 * * 目录:demo * ------------------------------------------------------------------- ...

  5. AES advanced encryption standard

    // advanced encryption standard // author: karl malbrain, malbrain@yahoo.com typedef unsigned char u ...

  6. Android - Mount a Samba share

    Mount Manager, Cifs manager :Manage your CIFS/NFS network shares was working, but the command from t ...

  7. linux(系统centos6.5)常用命令总结

    ls  -al 列出当前目录下的所有文件和子目录 用户在登录Linux时由/etc/passwd文件来决定要使用哪个shell,用户使用的shell被列于每行的末尾(/bin/bash) ls -F在 ...

  8. linux下安装MYSQL详细配置(转)

      #tar zxvf mysql-5.0.18.tar.gz#cd  mysql-5.0.18 #./configure --prefix=/usr/local/mysql --with-chars ...

  9. TCP Socket的一些行为

    几个重要的结论: 1. read总是在接收缓冲区有数据时立即返回,而不是等到给定的read buffer填满时返回. 只有当receive buffer为空时,blocking模式才会等待,而nonb ...

  10. 百度地图api改变覆盖物背景实例及css颜色值简介

    在此鸣谢buptwusuopu的技术支持 在调用百度地图api的时候,为了改变覆盖物的颜色,如图中椭圆型的填充色.可以到百度api的库中查找方法http://developer.baidu.com/m ...