public int candy(int[] ratings) {
        int[] candy = new int[ratings.length];
        //considering the minimal rating on the left, give 1 candy
        candy[0] = 1;
        //loop from second child
        for (int i = 1; i < ratings.length; i++) {
            //if value bigger than before, add 1, otherwise, set 1 and continue
            candy[i] = ratings[i] > ratings[i - 1] ? candy[i - 1] + 1 : 1;
        }
        //considering the minimal rating on the right
        int totalCandy = candy[ratings.length - 1];
        //loop from the child before the last one
        for (int i = ratings.length - 2; i >= 0; i--) {
            //if the left value bigger than the right one, also the given candy is not larger than right one
            //candy[i] = candy[i+1] + 1. (Cause if value bigger than neighbour, candy should be more)
            //otherwise, given candy no changes
            candy[i] = (ratings[i] > ratings[i + 1] && candy[i + 1] + 1 > candy[i]) ? candy[i + 1] + 1 : candy[i];
            //count total candies by the way
            totalCandy += candy[i];
        }
        return totalCandy;
    }