@@ -0,0 +1,1309 @@
+/*%0A   There are N children standing in a line. Each child is assigned a rating value.%0A%0A%09You are giving candies to these children subjected to the following requirements:%0A%09%0A%09Each child must have at least one candy.%0A%09Children with a higher rating get more candies than their neighbors.%0A%09What is the minimum candies you must give?%0A%09%0A%09Analysis:%0A%09  2 loop%0A%09  time O(n)%0A */%0Apublic class Candy %7B%0A%09  public static int candy(int%5B%5D ratings) %7B%0A%09%09    if(ratings == null %7C%7C ratings.length==0)%0A%09%09    %7B%0A%09%09        return 0;%0A%09%09    %7D%0A%09%09    int%5B%5D nums = new int%5Bratings.length%5D;%0A%09%09    nums%5B0%5D = 1;%0A%09%09    %0A%09%09    for(int i = 1; i %3C ratings.length; i++)%0A%09%09    %7B%0A%09%09        if(ratings%5Bi%5D %3E ratings%5Bi-1%5D)%0A%09%09        %7B%0A%09%09            nums%5Bi%5D = nums%5Bi-1%5D+1;%0A%09%09        %7D%0A%09%09        else%0A%09%09        %7B%0A%09%09            nums%5Bi%5D = 1;%0A%09%09        %7D%0A%09%09    %7D%0A%09%09    int res = nums%5Bratings.length-1%5D;  %0A%09%09    %0A%09%09    for(int i = ratings.length-2; i %3E= 0; i--)%0A%09%09    %7B%0A%09%09        int cur = 1;%0A%09%09        if(ratings%5Bi%5D %3E ratings%5Bi+1%5D)%0A%09%09        %7B%0A%09%09            cur = nums%5Bi+1%5D+1;%0A%09%09        %7D%0A%09%09        res += Math.max(cur,nums%5Bi%5D);%0A%09%09        nums%5Bi%5D = cur;%0A%09%09    %7D%0A%09%09    return res;%0A%09%09%7D%0A%09public static void main(String%5B%5D args) %7B%0A%09%09// TODO Auto-generated method stub%0A%09%09int%5B%5D rateArray = %7B1,2,4,1,3%7D;%0A%09%09int res = candy(rateArray);%0A%09%09System.out.print(res);%0A%09%7D%0A%0A%7D%0A

Create Candy135.java