首页 > 代码库 > Candy leetcode java

Candy leetcode java

题目

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++){  
10             if(ratings[i]>ratings[i-1])  
11                 leftnums[i] = leftnums[i-1]+1;  
12             else  
13                 leftnums[i] = 1;  
14         }
15         
16         rightnums[ratings.length-1] = leftnums[ratings.length-1];  
17         for(int i=ratings.length-2;i>=0;i--){
18             if(ratings[i]>ratings[i+1]) 
19                 rightnums[i] = rightnums[i+1]+1;
20             else
21                 rightnums[i] = 1;
22                 
23         }
24         
25         int res = 0;
26         for(int i = 0; i<ratings.length; i++)
27             res += Math.max(leftnums[i],rightnums[i]);
28         
29         return res;  
30     }