I've read that a greedy algorithm only take cares for the optimal solution that is trying to reach at that moment, but is this the only criteria I should consider if I want to create a greedy algorithm? Aslo, how can I know if I have created a greedy algorithm or not? I mean, I created the following code for the problem of the change in C++
:
#include <iostream>
using namespace std;
int greedyChange(int coinSet[], int lenCoinSet, int money){
int change = 0;
static int i = 0;
if(i >= lenCoinSet){
return 0;
}
while(money - coinSet[i] >= 0){
change++;
money -= coinSet[i];
}
i++;
return change + greedyChange(coinSet, lenCoinSet, money);
}
int main(int argc, char const *argv[]){
int coinSet[]={20, 15, 10, 5, 1};
int lenCoinSet = sizeof(coinSet)/sizeof(coinSet[0]);
int money = 30;
cout << "The minimun number of coins to get your change is: "
<< greedyChange(coinSet, lenCoinSet, money)<<endl;
return 0;
}
And I think it is greedy, but I'm not sure. I would apreciate if you could explain me if the code I wrote is greedy or not. Besides, if it isn't, do you have another possible solution you could share or maybe some advices to improve this code? Finally, if there is documentation you could recommend me, I'll be very thankful.