Dynamic programming(斐波那契数列,找零钱)

斐波那契数列

斐波那契额数列:1,1,2,3,5,8,13,21,34
设计一个函数,求第n位上的数。

1.暴力解 很简单的方法:

def fib(n):
    if n == 1 or n == 2:
        return 1
    else:
        return fib(n - 1) + fib(n - 2)

但是,这个方法有一个很大的问题,计算重复太多,例如,我们要算fib(5)

  • fib(5)
    • fib(4)
      • fib(3)
        • fib(2)
        • fib(1)
      • fib(2)
    • fib(3)
      • fib(2)
      • fib(1)

2.DP algorithm

可以发现,当中有大量的重复计算。我们可以设计一个备忘录,将算过的数值保存进去。

dic = {}
def fib2(n):
    if n in dic:
        return dic[n]
    if n == 0:
        return 0
    if n == 1:
        return 1
    value = fib2(n - 1) + fib2(n - 2)
    dic[n] = value
    return value

3.自下而上的DP algorithm

或者,从下而上进行动态规划

dp = []
def fib3(n):
    dp = [0] * (n + 1)
    if n == 0:
        return 0
    dp[1] = 1
    for i in range(2, n + 1):
        dp[i] = dp[i - 1] + dp[i - 2]
    return dp[n]

比较fib(40),三种方法的时间分别是

27.43297505378723
3.62396240234375e-05
1.5020370483398438e-05

我们发现,时间成指数下降

我们发现,其实,f(n) 只和前两个数字有关,所以,第三种方法没有必要设置一个n+1长的盒子来保存数字,将fib3优化:

def fib4(n):
    if n == 2 or n == 1:
        return 1
    prev = 1
    curr = 1
    for i in range(3, n + 1):
        sum = prev + curr
        prev = curr
        curr = sum
    return curr

时间进一步降低:

6.9141387939453125e-06

找零钱

找零钱问题,给一个零钱数列:[1,2,5],现在如果有11块钱,问最少几枚硬币能凑出这个数额。

1.暴力解

如果要凑11,那么如果知道怎么凑10,就知道怎么凑11,因为有10,再加上一枚硬币就可以得到11。


递归树

但是需要选择硬币最少的那个结果:

def coinChange(amount):
    if amount == 0:
        return 0
    if amount < 0:
        return -1
    res = float('INF')
    for coin in coins:
        subproblem = coinChange(amount - coin)
        if subproblem == -1: continue
        # choose the min because we need the best choose, least coins --> minimum amount
        res = min(res, 1 + subproblem)
    return res if res != float('INF') else -1

2.DP algorithm

同上面斐波那契数列一样,列一个备忘录(memo)

memo = {}


def coinChange2(amount):
    if amount in memo:
        return memo[amount]
    if amount == 0:
        return 0
    if amount < 0:
        return -1
    res = float('INF')
    for coin in coins:
        subproblem = coinChange(amount - coin)
        if subproblem == -1: continue
        res = min(res, 1 + subproblem)
    memo[res] = res if res != float('INF') else -1
    return memo[res]

3.自下而上的DP algorithm

def coinChange3(amount):
    dp = [amount + 1] * (amount + 1)
    dp[0] = 0
    for item in range(len(dp)):
        for coin in coins:
            if (item - coin < 0): continue
            dp[item] = min(dp[item], 1 + dp[item - coin])
            print(dp)
    return -1 if (dp[amount] == (amount + 1)) else dp[amount]

可以发现,每一次的结果:

[0, 1, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12]
[0, 1, 2, 12, 12, 12, 12, 12, 12, 12, 12, 12]
[0, 1, 1, 12, 12, 12, 12, 12, 12, 12, 12, 12]
[0, 1, 1, 2, 12, 12, 12, 12, 12, 12, 12, 12]
[0, 1, 1, 2, 12, 12, 12, 12, 12, 12, 12, 12]
[0, 1, 1, 2, 3, 12, 12, 12, 12, 12, 12, 12]
[0, 1, 1, 2, 2, 12, 12, 12, 12, 12, 12, 12]
[0, 1, 1, 2, 2, 3, 12, 12, 12, 12, 12, 12]
[0, 1, 1, 2, 2, 3, 12, 12, 12, 12, 12, 12]
[0, 1, 1, 2, 2, 1, 12, 12, 12, 12, 12, 12]
[0, 1, 1, 2, 2, 1, 2, 12, 12, 12, 12, 12]
[0, 1, 1, 2, 2, 1, 2, 12, 12, 12, 12, 12]
[0, 1, 1, 2, 2, 1, 2, 12, 12, 12, 12, 12]
[0, 1, 1, 2, 2, 1, 2, 3, 12, 12, 12, 12]
[0, 1, 1, 2, 2, 1, 2, 2, 12, 12, 12, 12]
[0, 1, 1, 2, 2, 1, 2, 2, 12, 12, 12, 12]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 12, 12, 12]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 12, 12, 12]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 12, 12, 12]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 4, 12, 12]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 12, 12]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 12, 12]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 4, 12]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 4, 12]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 2, 12]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 2, 3]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 2, 3]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 2, 3]

dynamic programming 就是一种聪明的穷举

你可能感兴趣的:(Dynamic programming(斐波那契数列,找零钱))