[leetcode] Candy

题目:(Greedy)

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?

题解:


参考引用 http://www.cnblogs.com/springfor/p/3877120.html

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

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

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

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

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

public class Solution {
    public int candy(int[] ratings) {
        if(ratings.length==0||ratings==null)
           return 0;
        
        int [] leftMost = new int [ratings.length];
        int [] rightMost = new int [ratings.length];
        
        leftMost[0]=1;
        for(int i=1;i<ratings.length;i++)
        {
            if(ratings[i]>ratings[i-1])
              leftMost[i]=leftMost[i-1]+1;
            else
              leftMost[i]=1;
        }
        
        rightMost[ratings.length-1]=1;
        for(int i=ratings.length-2; i>=0;i--)
        {
            if(ratings[i]>ratings[i+1])
              rightMost[i]=rightMost[i+1]+1;
            else
              rightMost[i]=1;
        }
        
        int res=0;
        for(int i=0; i<ratings.length; i++)
            res+=Math.max(leftMost[i],rightMost[i]);
        
        return res;
    }
}
原文地址:https://www.cnblogs.com/fengmangZoo/p/4185952.html