Solving Minimum Coin Change

For many people that are preparing for coding interviews, the fear of dynamic scheduling ( DP ) questions is quite common. This is in partially ascribable to the type of problems that much requires DP to solve. These questions much concern combinatorics and repetitive calculations. now, if person was learning about DP, it ’ s likely that they would come across the minimum coin change trouble ( or at least a variant of it ). In this mail, we ’ ll discuss the logic involved in solving this trouble. But before we can dive into the problem, let ’ s spill a moment about what moral force scheduling is.

A Brief Explanation of DP

here ’ s a Quora answer that I believe nails the philosophy of dynamic programming :

Simply put, DP is a method in which we store previously calculated values so that we can easily retrieve them again without having to recalculate .

Being able to store values allows us to quickly retrieve them and use as smaller sub-solutions to solve for flush larger ones. now, I believe what makes DP problems therefore frightening is not so much the method of border on, but the nature of the trouble and precisely how to apply DP. In order to better understand it, let ’ s count at the minimal coin exchange problem .

Defining the Problem

The minimum coin change trouble goes a succeed :

Suppose you’re given an array of numbers that represent the values of each coin.* Then you’re given an amount and asked to find the minimum number of coins that are needed to make that amount.

* Assume the number of coins you have are infinite, so you don’t need to worry about how many coins are at your disposal. Example :

Coins: [ 1, 2, 5 ] Amount: 11 Answer : 3 coins ( because 5 + 5 + 1 = 11 )

Approaching the Problem

From a glance, this problem seems truly daunting. An initial think might be to determine which of the combinations of coins will have the minimum numeral of coins. Using this think, we ’ vitamin d determine that 11 can be made up in the watch ways :

• 1 + 1 + 1 + … + 1 = 11
• 1 + 1 + 1 + 1 + 1 (9 ones) + 2 = 11
• 5 + 5 + 1= 11

now, this approach looks rather dim-witted. But as you can see, I got tired of writing the assorted combination of coins. merely imagine if we have a larger array of coins and an even larger total. This approach would be airy. As a matter of fact, it ’ mho unnecessary and long-winded to record all the combination of coins. But fortunately, there ’ s a much elementary and arguably more elegant solution .

DP Approach

Recall the Quora answer from earlier. Let ’ s start with : 1 + 1 + 1 +1 + … + 1 = 10 now, the first time, it may take a while to add up all the 1s to get 10. But, if I was to ask you “ How could you make 11 ? ”, you would be able to tell me right away that all we have to do is : 10+ 1 = 11 .

Remember, DP consists of storing previously calculated values to prevent repetitive calculations and allow for quick retrievals as well as using smaller values to solve for even larger ones.

But precisely how are we going to store our previous calculations ? well, we could use an array. Suppose that we have this align of numbers, that will hold the minimum number of coins for each amount, starting from 0 to the total, which in this font is 11 :Each value in the array represents the minimum number of coins for each amount nowadays the question we face is, what values do we initialize each index with ? Well, since we ’ rhenium dealing with minimums, values are frequently initialized to Infinity ( ∞ ). The logic is that at this moment, the minimum total of coins to make each amount is infinite : But, there’s one important thing to note. Assuming that we ’ rhenium only given positive value coins, we know that it is impossible to make an sum of 0 using any of the coins. so, we can say that there are 0 ways of making 0 :There are 0 ways to make an amount of 0 with positive coin values Okay, now let ’ s expect at each coin separately to see how many of a specific mint can make each total. Since our first mint is the 1-coin, we ’ rhenium going to ask : “ Using just 1-coins, how many coins does it take to make a value of sum : 1 ? 2 ? … 11 ? ” first, let ’ s ask : “ From 0, how many 1-coins do I need to make 1 ? ”

• 1 + 1 + 1 + 1 = 4 (only 1s)
• 1 + 1 + 2 = 4 (both 1 and 2)
• 2 + 2 = 4 (all 2s)

As we can see, that using only two 2-coins will give us the minimal number of coins it takes to make the total 4. Or another way to put it, from the amount 2, we can add a 2-coin to make 4 : From total 6, we can add a 2-coin. That will give us an total of 8. Since the minimum number of coins needed to make 6 is 3 ( 2 + 2 + 2 ), the modern minimum number of ways to make 8 is by putting a 2-coin on top of the amount 6, thus making it 4 coins. now that we ’ ve finished looking at the minimal numeral of coins needed to make each total using 1-coins and 2-coins, let ’ s look at 5-coins : It ’ mho becoming open that the first controversy in the min() is array[current_amount — current_coin] + 1, while the second argument is just the array[current_amount]. And at survive, we ’ ve exhausted all our coin options and see that at amount 11, there is a minimal value of 3 coins ( 5 + 5 + 1 ) that are required to make 11. now, let ’ s count at the code .

Code

The code is written as follow using JavaScript :

Analysis

From the code, we can see that we ’ re using two for-loops. Since we ’ re iterate over the entire minCoins ( which has a length peer to amount ) array every time we iterate over an element in the coins array, we say that the runtime of this algorithm is O(|coins|•|amount|) where |coins| is the length of the coins range and |amount| is the length of the minCoins array. And since we ’ rhenium using an align to keep cut of our minimum coins for each measure, minCoins, the space complexity is O(|amount|) .

Summary

• Dynamic Programming (DP) is simply the method of storing previously calculated values so that we don’t have to recalculate them, which saves us time and allows us to use smaller sub-solutions to solve larger ones.
• Look at one coin at a time and find out what is the minimum number of coins that are needed to make each amount from 0 to amount.
• Runtime: O(|coins|•|amount|), where |coins| represent the length of the coins array and |amount| represents the length of minCoins array.
• Space Complexity: O(|amount|) because we used an array to keep track of the minimum number of coins for each amount.
reference : https://ontopwiki.com
Category : Finance