Created
November 5, 2019 18:32
-
-
Save adamkorg/6308ae6d8c7dfeb42d45c020022b9af3 to your computer and use it in GitHub Desktop.
Leetcode 135: Candy (brute force)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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