[Python] Understanding change-making algorithm

Discussão em 'Python' iniciado por Stack, Janeiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    I was looking for a good solution to the Change-making problem and I found this code(Python):

    target = 200
    coins = [1,2,5,10,20,50,100,200]
    ways = [1]+[0]*target
    for coin in coins:
    for i in range(coin,target+1):
    ways+=ways[i-coin]
    print(ways[target])


    I have no problems understanding what the code literally does,but I can't understand WHY it works. Anyone can help?

    Continue reading...

Compartilhe esta Página