Simple O(n) Java solution with comments
public int candy(int[] ratings) {\n        int candies[] = new int[ratings.length];        \n        Arrays.fill(candies, 1);// Give each child 1 candy \n      
    public int candy(int[] ratings) {\n        int candies[] = new int[ratings.length];        \n        Arrays.fill(candies, 1);// Give each child 1 candy \n        \t\n        for (int i = 1; i < candies.length; i++){// Scan from left to right, to make sure right higher rated child gets 1 more candy than left lower rated child\n\t        if (ratings[i] > ratings[i - 1]) candies[i] = (candies[i - 1] + 1);\n\t    }\n\t     \n\t    for (int i = candies.length - 2; i >= 0; i--) {// Scan from right to left, to make sure left higher rated child gets 1 more candy than right lower rated child\n\t\t    if (ratings[i] > ratings[i + 1]) candies[i] = Math.max(candies[i], (candies[i + 1] + 1));\n\t    }\n\t    \n        int sum = 0;        \n        for (int candy : candies)  \n        \tsum += candy;        \n        return sum;\n\t}
[]