Understanding change-making algorithm

后端 未结 4 1956
礼貌的吻别
礼貌的吻别 2020-12-01 08:07

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]*ta         


        
4条回答
  •  渐次进展
    2020-12-01 09:00

    This is a classical example of dynamic programming. It uses caching to avoid the pitfall of counting things like 3+2 = 5 twice (because of another possible solution: 2+3). A recursive algorithm falls into that pitfall. Let's focus on a simple example, where target = 5 and coins = [1,2,3]. The piece of code you posted counts:

    1. 3+2
    2. 3+1+1
    3. 2+2+1
    4. 1+2+1+1
    5. 1+1+1+1+1

    while the recursive version counts:

    1. 3+2
    2. 2+3
    3. 3+1+1
    4. 1+3+1
    5. 1+1+3
    6. 2+1+2
    7. 1+1+2
    8. 2+2+1
    9. 2+1+1+1
    10. 1+2+1+1
    11. 1+1+2+1
    12. 1+1+1+2
    13. 1+1+1+1+1

    Recursive code:

    coinsOptions = [1, 2, 3]
    def numberOfWays(target):
        if (target < 0):
            return 0
        elif(target == 0):
            return 1
        else:
            return sum([numberOfWays(target - coin) for coin in coinsOptions])
    print numberOfWays(5)
    

    Dynamic programming:

    target = 5
    coins = [1,2,3]
    ways = [1]+[0]*target
    for coin in coins:
        for i in range(coin, target+1):
            ways[i]+=ways[i-coin]
    print ways[target]
    

提交回复
热议问题