A country has coins with k denominations 1 = d1 < d2 < ... < dk, and you want to make change for n cents using the smallest number of coins. For example, in the United States we have d1 = 1, d2 = 5, d3 = 10, d4 = 25, and the change for 37 cents with the smallest number of coins is 1 quarter, 1 dime, and 2 pennies, which are a total of 4 coins. To solve for the general case (change for n cents with k denominations d1 ... dk), we refer to dynamic programming to design an algorithm. 1. We will come up with sub-problems and recursive relationship for you. Let be the minimum number of coins needed to make change for n cents, then we have: Explain why the above recursive relationship is correct. [Formal proof is not required] 2. Use the relationship above to design a dynamic programming algorithm, where the inputs include the k denominations d1 ... dk and the number of cents n to make changes for, and the output is the minimum number of coins needed to make change for n. Provide the pseudocode of your algorithm and briefly justify the runtime of your algorithm using big-O notation. 3. Adapt your algorithm above by tracking some useful information during the DP procedure, so that it returns the actual method (i.e., the number of coins for each denomination) to change for n cents, not just the minimum number of denominations.
A country has coins with k denominations 1 = d1 < d2 < ... < dk, and you want to make change for n cents using the smallest number of coins.
For example, in the United States we have d1 = 1, d2 = 5, d3 = 10, d4 = 25, and the change for 37 cents with the smallest number of coins is 1 quarter, 1 dime, and 2 pennies, which are a total of 4 coins.
To solve for the general case (change for n cents with k denominations d1 ... dk), we refer to dynamic
1. We will come up with sub-problems and recursive relationship for you. Let be the minimum number of coins needed to make change for n cents, then we have:
Explain why the above recursive relationship is correct. [Formal proof is not required]
2. Use the relationship above to design a dynamic programming algorithm, where the inputs include the k denominations d1 ... dk and the number of cents n to make changes for, and the output is the minimum number of coins needed to make change for n.
Provide the pseudocode of your algorithm and briefly justify the runtime of your algorithm using big-O notation.
3. Adapt your algorithm above by tracking some useful information during the DP procedure, so that it returns the actual method (i.e., the number of coins for each denomination) to change for n cents, not just the minimum number of denominations.
Step by step
Solved in 4 steps