Skip to content

Instantly share code, notes, and snippets.

@adamkorg
Created November 5, 2019 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamkorg/6308ae6d8c7dfeb42d45c020022b9af3 to your computer and use it in GitHub Desktop.
Save adamkorg/6308ae6d8c7dfeb42d45c020022b9af3 to your computer and use it in GitHub Desktop.
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