518.Coin Change 2

num of solutions

The most interesting line is: This line calculates the number of ways to get amount of money by using coins. The number is calculated by adding number of ways it can be done without using coin "coin" plus the number of ways it can be done with the coin "coin". The number of ways to get without using "coin" is which was already calculated while looping through other coins. The other way to get is to add "coin" to . So, it's just a matter of finding how many ways there are to get . It is actually that was already calculated while looping through smaller values. So, as a result (number of ways to get j without using "coin") (all other ways to get j).

def make_change(coins, n):
results = [0 for _ in range(n + 1)]
results[0] = 1
for coin in coins:
for i in range(coin, n + 1):
results[i] += results[i - coin]
return results[n]

results matching ""

    No results matching ""