Simple BFS
Idea: \n1. Find the worst-rated children and start with them\n2. For Each worst child, give them just one candy (c)\n3. For each of neighbour, \n\t- If they hav
Idea: \n1. Find the worst-rated children and start with them\n2. For Each worst child, give them just one candy (c)\n3. For each of neighbour, \n\t- If they have a  higher rating, give them c + 1 candies\n\t- Otherwise give them 1 candy\n4. If ever there is a conflict, round up.\n\nThe code is a straightforward breadth-first search. O(n) space and time. \n\n```\nvar candy = function(ratings) {\n    \n    const result = new Array(ratings.length).fill(0);\n    const min = Math.min(...ratings, 0);\n\n    const q = [];\n    ratings.forEach((r, i) => {\n        if(r === min) q.push([i, 1]);\n    });\n    \n    while(q.length){    \n        const [index, candies] = q.shift();\n        \n        if(result[index] < candies) result[index] = candies;\n        else continue;\n        \n        if(index > 0){\n            const left = index - 1;\n            const leftCandy = ratings[left] > ratings[index] ? (candies+1) : 1;\n            if(leftCandy > result[left]) q.push([left, leftCandy]);\n        }\n        \n        if(index < ratings.length-1){\n            const right = index + 1;\n            const rightCandy = ratings[right] > ratings[index] ? (candies+1) : 1;\n            if(rightCandy > result[right]) q.push([right, rightCandy]);\n        }\n    }\n    \n    return result.reduce((a, b) => a + b, 0);\n};\n```
['Breadth-First Search', 'Graph', 'JavaScript']