public int candy(int[] ratings){
  int[] candies=new int[ratings.length];
  candies[0]=1;
  for (int i=1; i < ratings.length; i++)   candies[i]=ratings[i] > ratings[i - 1] ? candies[i - 1] + 1 : 1;
  int res=candies[candies.length - 1];
  for (int i=ratings.length - 2; i >= 0; i--) {
    if (ratings[i] > ratings[i + 1])     candies[i]=Math.max(candies[i],candies[i + 1] + 1);
    res+=candies[i];
  }
  return res;
}

public int candy ( int [ ] ratings ) { int [ ] candies = new int [ ratings . length ] ; candies [ 0 ] = 1 ; for ( int i = 1 ; i < ratings . length ; i ++ ) candies [ i ] = ratings [ i ] > ratings [ i - 1 ] ? candies [ i - 1 ] + 1 : 1 ; int res = candies [ candies . length - 1 ] ; for ( int i = ratings . length - 2 ; i >= 0 ; i -- ) { if ( ratings [ i ] > ratings [ i + 1 ] ) candies [ i ] = Math . max ( candies [ i ] , candies [ i + 1 ] + 1 ) ; res += candies [ i ] ; } return res ; }
o ( n ) time , o ( n ) space from left to right , if ratings [ i ] increase , give one more from right to left , if ratings [ i ] increase , give one more answer should be the max of two array simplify second traversal by calculate and decide max right away and also calculate the sum
