Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Last active June 25, 2020 19:52
Show Gist options
  • Save misterpoloy/e97ff714d0136bc0daa1e403c8f451fe to your computer and use it in GitHub Desktop.
Save misterpoloy/e97ff714d0136bc0daa1e403c8f451fe to your computer and use it in GitHub Desktop.
#CodeChallenge Given a set of coins denominations calculate the minimum combinations to get the target amount
#include <vector>
#include <climits>
using namespace std;
// O(dn) time | O(n) space
int minNumberOfCoinsForChange(int n, vector<int> denoms) {
// Create a matrix, to store the best possiblitites until target.
vector<int> numOfCoins(n + 1, INT_MAX); // +1, to include zero.
numOfCoins[0] = 0; // Starts at zero because, how many 0 coins do I need to make 0 in value.
for (int denomination : denoms) {
// Create the matrix to store the best combination so far
for (int amountTarget = 0; amountTarget < numOfCoins.size(); amountTarget++) {
// Is possible to make it with that bill?
if (denomination <= amountTarget) {
// if I can't create the number in the past, then keep infinity to avoid flip C++ SHIT. (if int_max + 1 == min_int)
int newTarget = numOfCoins[amountTarget - denomination];
int toCompare = newTarget == INT_MAX ? newTarget : newTarget + 1;
// Should I include or exclude the coin?, keep the min
numOfCoins[amountTarget] = min(numOfCoins[amountTarget], toCompare);
}
}
}
return numOfCoins[n] != INT_MAX ? numOfCoins[n] : -1;
}
@misterpoloy
Copy link
Author

misterpoloy commented Jun 25, 2020

CodeChallenge

CodeChallenge Coins

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment