Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Leetcode 135: Candy (brute force)
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int candy(vector<int>& ratings) {
vector<int> vc(ratings.size(), 1);
bool change = true;
while (change) {
change = false;
for (int i=0; i<vc.size(); ++i) {
if ((i>0 && ratings[i]>ratings[i-1] && vc[i]<=vc[i-1]) ||
(i<vc.size()-1 && ratings[i]>ratings[i+1] && vc[i]<=vc[i+1])) {
vc[i]++;
change = true;
}
}
}
int sum = accumulate(vc.begin(), vc.end(), 0);
return sum;
}
int main() {
vector<int> ratings {1,0,2};
cout << candy(ratings) << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.