Skip to content

Instantly share code, notes, and snippets.

@ygabo
Created February 24, 2014 04:55
Show Gist options
  • Save ygabo/9182158 to your computer and use it in GitHub Desktop.
Save ygabo/9182158 to your computer and use it in GitHub Desktop.
minimum number of coins to make change, n.
void change(vector<int>* coins, int n)
{
vector<int> table(n + 1);
table[0] = 1;
table[1] = 1;
for (int i = 2; i < n + 1; i++)
{
int min = 0;
int prev = 0;
for (auto c = coins->begin(); c != coins->end(); ++c)
{
prev = i - *c;
if (prev < 0)
continue;
if (0 == min)
min = table[prev];
if (i == *c){
min = 0;
break;
}
if (min >= table[prev]){
min = table[prev];
}
}
table[i] = min + 1;
}
cout << n << " -> " << table[n] << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment