One Pass Betch 😎
Intuition\n Describe your first thoughts on how to solve this problem. \n You can really put two brain cells together to see that two loops are running same num
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n You can really put two brain cells together to see that two loops are running same number of times, just one forward and one backward, so we can fit that into one loop and not be a betch about it.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe basic thing is whenever we see any rating of a child with higher rating than neighbour, we need on more candy than that neighbour, if not that child is already getting more, so we store the maximized value of candies for each child in the vector. Lastly, we return the sum with a little accumulate function and save ourselves from being a betch again by nothaving to write another 3-4 lines of code.\n\n# Complexity\n- Time complexity: $$O(n)$$ where n is the number of children in your basement.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$ an extra array to calculate the result, technically the array is the result, but sneekily these mfs asked for the sum only, so this array counts as extra space.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int candy(vector<int>& r) {\n        int n=r.size();vector<int> v(n,1);\n        for(int i=1;i<n;i++){   int j=n-i-1;\n            if(r[i]>r[i-1]) v[i]=max(v[i],v[i-1]+1);\n            if(r[j]>r[j+1]) v[j]=max(v[j],v[j+1]+1);\n        }return(accumulate(v.begin(),v.end(),0));\n    }\n};\n```\n\n_**UPVOTE IF NOT BETCH**_
['Array', 'Greedy', 'C++']