Last active
June 25, 2020 19:52
-
-
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
This file contains hidden or 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
| #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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.