The problem of finding the minimum number of coins required to make a given amount using dynamic programming. The greedy method and its limitations, and proposes a dynamic programming solution. The example uses coins of denominations 1¢, 3¢, and 4¢.
Download Dynamic Programming: Minimum Number of Coins for Given Amount and more Study notes Digital & Analog Electronics in PDF only on Docsity!
Dynamic Programming
Counting coins
(^) To find the minimum number of US coins to make any
amount, the greedy method always works
(^) At each step, just choose the largest coin that does not overshoot the desired amount: 31¢=
(^) The greedy method would not work if we did not have
5 ¢ coins
(^) For 31 cents, the greedy method gives seven coins (25+1+1+1+1+1+1), but we can do it with four (10+10+10+1)
(^) The greedy method also would not work if we had a 21¢
coin
(^) For 63 cents, the greedy method gives six coins (25+25+10+1+1+1), but we can do it with three (21+21+21)
(^) How can we find the minimum number of coins for any
given coin set?
A dynamic programming
solution
(^) Idea: Solve first for one cent, then two cents, then three cents, etc., up to the desired amount - (^) Save each answer in an array!
(^) For each new amount N, compute all the possible pairs of previous answers which sum to N - (^) For example, to find the solution for 13¢, - (^) First, solve for all of 1¢, 2¢, 3¢, ..., 12¢ - (^) Next, choose the best solution among: - (^) Solution for 1¢ + solution for 12¢ - (^) Solution for 2¢ + solution for 11¢ - (^) Solution for 3¢ + solution for 10¢ - (^) Solution for 4¢ + solution for 9¢ - (^) Solution for 5¢ + solution for 8¢ - (^) Solution for 6¢ + solution for 7¢
Example
(^) Suppose coins are 1¢, 3¢, and 4¢
(^) There’s only one way to make 1¢ (one coin)
(^) To make 2¢, try 1¢+1¢ (one coin + one coin = 2 coins)
(^) To make 3¢, just use the 3¢ coin (one coin)
(^) To make 4¢, just use the 4¢ coin (one coin)
(^) To make 5¢, try
(^) 1¢ + 4¢ (1 coin + 1 coin = 2 coins)
(^) 2¢ + 3¢ (2 coins + 1 coin = 3 coins)
(^) The first solution is better, so best solution is 2 coins