Created
February 24, 2014 04:55
-
-
Save ygabo/9182158 to your computer and use it in GitHub Desktop.
minimum number of coins to make change, n.
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
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