题目

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. 查看shell 版本

    cat /etc/shells 查看本机支持的解释器: echo $SHELL 当我们直接使用./a.sh来执行这个脚本的时候,如果没有shebang,那么它就会默认用$SHELL指定的解释器,否则就 ...

  2. Top 5 SSH Clients for Windows (Alternatives of PuTTY)

    这篇博文列举了可以替代putty的5个工具,有些实现了putty没有实现的一些功能.如下: PuTTy is the most popular SSH clients for Windows-base ...

  3. CSS3组件化之单线箭头

    <div class="parent-box"> <div class="top-arrow"></div> <div ...

  4. LNMP一键安装包如何重装Nginx

    LNMP一键安装包安装好后,相应的Mysql,Nginx及PHP都会安装配置完成. 由于某些特殊情况的需要,如何更换Nginx的版本呢? nginx升级脚本可以完成. 1. 手动编译方法:/usr/l ...

  5. Java并发(二十二):定时任务ScheduledThreadPoolExecutor

    需要在理解线程池原理的基础上学习定时任务:Java并发(二十一):线程池实现原理 一.先做总结 通过一个简单示例总结: public static void main(String[] args) { ...

  6. Android MediaCodec 的实例化方法

    *由于作者水平限制,文中难免有错误和不恰当之处,望批评指正. *转载请注明出处:http://www.cnblogs.com/roger-yu/ MediaCodec的实例化方法主要有两种: 1.使用 ...

  7. Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划

    A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...

  8. SQL Server Management Studio 教程一:设置sa用户登录

    今天在net项目中添加数据库过程中出现了小问题,就是使用sql server身份验证没登录成功,经过一番调试,终于解决问题. 使用sa账户登录sql server 2008 的方法步骤如下: 1.首先 ...

  9. php在linux后台执行

    <?php ignore_user_abort();//后台运行 ini_set('default_socket_timeout', -1);//socket不超时 set_time_limit ...

  10. Git_操作标签

    如果标签打错了,也可以删除: $ git tag -d v0.1 Deleted tag 'v0.1' (was e078af9) 因为创建的标签都只存储在本地,不会自动推送到远程.所以,打错的标签可 ...