C++ Easy O(n) Solution , Using 2 only for loop
Intuition  \n   If neighbour rating is less we should give that child one more candy.\n Describe your first thoughts on how to solve this problem. \n\n# Approac
# Intuition  \n   If neighbour rating is less we should give that child one more candy.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nAt initials - give 1 candy to every child to meet the condition - every child must get a candy.\n\nTHEN \n\n    for loop ---------------->\n      If ratings[i]>ratings[i-1]\n            i+1 th child get one more candy than i th child,so increase it once\n     for loop <-----------------\n      If ratings[i]>ratings[i+1]\n           i th child get one more candy than i+1 th child, so increase it once \n\n\nOne more thing you have to keep in mind that, while reverse traversing in 2nd loop you should not give a increment to a child who already got in first loop and not required increment anymore\n\n         example :-\n     ratings              1 3 2 2 1\n     candy get:-\n     intitials            1 1 1 1 1\n     after 1st loop       1 2 1 1 1\n     after 2nd loop       1 3 1 2 1\n                            ^\n                            |\n                        got increment twice, but dont need in 2nd loop\n    so while reverse traversing we also check\n               candy[i]<=candy[i+1]\n                              then only it get a increment.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int candy(vector<int>& ratings) {\n        if(ratings.size()==1){\n            return 1;\n        }\n        \n        vector<int>ans(ratings.size(),1);  \n        for(int i=1;i<ratings.size();i++){\n            if(ratings[i]>ratings[i-1]){\n                ans[i]=ans[i-1]+1;\n            }\n        }\n        for(int i=ratings.size()-2;i>=0;i--){\n            if(ratings[i]>ratings[i+1] && ans[i]<=ans[i+1]){\n                ans[i]=ans[i+1]+1;\n            }\n        }\n      \n     return accumulate(ans.begin(),ans.end(),0LL);\n    }\n};\n```
['C++']