Candy Distribution || C++ || 99% faster || explained
Used greedy aproach:\n1. every student should get atleast one so initially give 1 candy to every student\n2. Now traverse left to right, whenever we get a highe
Used greedy aproach:\n1. every student should get atleast one so initially give 1 candy to every student\n2. Now traverse left to right, whenever we get a higher rating, it should get more candy from its neighbour( left neighbour in the curr case) so give just one more candy than its left neighbour\n3. Now another case left when its right neighbour has higher rating, traverse from right, and if we get a higher rating, give it max(curr, 1+candy of its right). max ensures that for local maxima candies shuld be high than both neighbours;\n\n```\nint candy(vector<int>& r) {\n        int n = r.size();\n        vector<int> c(n, 1);\n        for (int i = 1; i < n; i++)\n            if (r[i] > r[i - 1]) c[i] = c[i - 1] + 1;\n        for (int i = n - 2; ~i; i--)\n            if (r[i] > r[i + 1]) c[i] = max(c[i], c[i + 1] + 1);\n        int res = 0;\n        for (auto t: c) res += t;\n        return res;\n    }\n```\n**If you find it helpful, plz upvote**
['Greedy', 'C']