You are viewing a single comment's thread. Return to all comments →
C++ solution
long getWays(int n, const vector<long> &c, const int i, vector<vector<long>> &lookupTable) { if (lookupTable[n - 1][i] != -1) return lookupTable[n - 1][i]; long ways = 0; int faceValue = c[i]; if (n % faceValue == 0) ++ways; for (long amount = 0; amount < n; amount += faceValue) { if (i > 0) { long waysToCombineRest = getWays(n - amount, c, i - 1, lookupTable); ways += waysToCombineRest; } } lookupTable[n - 1][i] = ways; return ways; } long getWays(int n, vector<long> c) { sort(c.begin(), c.end()); vector<vector<long>> lookupTable(n, vector<long>(c.size(), -1)); return getWays(n, c, c.size() - 1, lookupTable); }
Seems like cookies are disabled on this browser, please enable them to open this website
The Coin Change Problem
You are viewing a single comment's thread. Return to all comments →
C++ solution