Easy and fast.
Intuition\nThis is array based question.\n\n# Approach\n1. Take candy array with size ratings and initialize with one.\n2. Move left to right and compare the ra
# Intuition\nThis is array based question.\n\n# Approach\n1. Take candy array with size ratings and initialize with one.\n2. Move left to right and compare the ratings and candy value if ratings high and candy value less or equal to then increase the candy array value.\n3. Move right to left and do same #2.\n4. Sum the values of candy array.\n\n# Complexity\n- Time complexity:\no(n)\n\n- Space complexity:\no(n)\n\n# Code\n```\n/**\n * @param {number[]} ratings\n * @return {number}\n */\nvar candy = function(ratings) {\n    let candy = new Array(ratings.length).fill(1);\n\n// left to right \n    for(let i=1;i<ratings.length;i++){\n        if(ratings[i]>ratings[i-1] && candy[i] <= candy[i-1]){\n            candy[i]=candy[i-1]+1;\n        }\n    }\n\n// right to left\n    for(let i=ratings.length-2;i>=0;i--){\n    if(ratings[i]>ratings[i+1] && candy[i] <= candy[i+1]){\n            candy[i]=candy[i+1]+1;\n        }\n    }\n\n    let sum = candy.reduce((cur,acc)=> acc=cur+acc);\n    return sum;\n};\n```
['Array', 'JavaScript']