Leetcode 135 Solution
This article provides solution to leetcode question 135 (candy)
Access this page by simply typing in "lcs 135" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/candy
Solution
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();
int candies[n];
candies[0] = 1;
for (int i = 1; i < n; i++)
if (ratings[i] > ratings[i - 1])
candies[i] = candies[i - 1] + 1;
else
candies[i] = 1;
for (int i = n - 2; i >= 0; i--)
if (ratings[i] > ratings[i + 1])
candies[i] = max(candies[i + 1] + 1, candies[i]);
return accumulate(&candies[0], &candies[n], 0);
}
};