package com.fishercoder.solutions;

/**
 * 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?
 */
public class _135 {

    public int candy(int[] ratings) {
        int[] candy = new int[ratings.length];
        for (int i = 0; i < ratings.length; i++) {
            candy[i] = 1;
        }

        for (int i = 0; i < ratings.length - 1; i++) {
            if (ratings[i] < ratings[i + 1]) {
                candy[i + 1] = candy[i] + 1;
            }
        }

        for (int i = ratings.length - 1; i > 0; i--) {
            if (ratings[i] < ratings[i - 1]) {
                candy[i - 1] = Math.max(candy[i - 1], candy[i] + 1);
            }
        }

        int sum = 0;
        for (int i : candy) {
            sum += i;
        }

        return sum;
    }

}

@@ -35,16 +35,29 @@
 %0A%0A/**%0A *
+ 135. Candy%0A%0A
  There a
@@ -395,16 +395,50 @@
 _135 %7B%0A%0A
+  public static class Solution1 %7B%0A
     publ
@@ -473,18 +473,16 @@
 %7B%0A      
-  
 int%5B%5D ca
@@ -512,18 +512,16 @@
 ength%5D;%0A
-  
       fo
@@ -553,36 +553,32 @@
 .length; i++) %7B%0A
-    
         candy%5Bi%5D
@@ -585,31 +585,27 @@
  = 1;%0A      
-  
 %7D%0A%0A
-  
       for (i
@@ -645,36 +645,32 @@
  i++) %7B%0A        
-   
- 
 if (ratings%5Bi%5D %3C
@@ -684,30 +684,24 @@
 s%5Bi + 1%5D) %7B%0A
-      
           ca
@@ -731,32 +731,26 @@
  1;%0A        
-    %7D%0A  
+%7D%0A
       %7D%0A%0A   
@@ -748,26 +748,24 @@
    %7D%0A%0A      
- 
- 
 for (int i =
@@ -803,28 +803,24 @@
 ) %7B%0A        
-    
 if (ratings%5B
@@ -848,30 +848,24 @@
 %7B%0A          
-   
-   
 candy%5Bi - 1%5D
@@ -905,20 +905,16 @@
 %5D + 1);%0A
-    
         
@@ -917,31 +917,27 @@
     %7D%0A      
-  
 %7D%0A%0A
-  
       int su
@@ -945,26 +945,24 @@
  = 0;%0A      
-  
 for (int i :
@@ -979,20 +979,16 @@
         
- 
-   
 sum += i
@@ -991,31 +991,27 @@
 += i;%0A      
-  
 %7D%0A%0A
-  
       return
@@ -1022,11 +1022,14 @@
 ;%0A    %7D%0A
+  %7D
 %0A%7D%0A

refactor 135