Dynamic Programming - 动态规划

Definition

动态规划是一种常用的优化问题求解策略。动态规划算法是一种递归算法,他的本质是通过子问题执行顺序的调度来降低算法的代价。

The core implementation of DP

Those who cannot remember the past are condemened to repeat it.

动态规划的核心是记住已经计算过的解,动态规划的方法有两种:

  1. 自顶向下的备忘录法
  2. 自底向上

An example for understanding DP

  1. Recursion and DP

In order to calculate the value of Fibonacci, both recursion and DP are optional.

Recursion:

def fib(n):
    if n<=0: return 0
    if n==1: return 1
    return fib(n-1) + fib(n-2)

Too many repetitive computation have been done and lead to inefficiency. Fortunately, DP can avoid the redundancy computation. For each f i b ( i ) fib(i) fib(i), it needs to be computated once.

**However, we should notice that it will increase space complexity.**
  1. Top-down variation
def fib(k):
    if k==1: return 1
    if k==0: return 0
    ans = [-1 for i in range(k+1)]
    ans[1] = 1
    ans[0] = 0
    def fcci(ans, n):
        if n==1 or n==0 or ans[n]!=-1: return ans[n]
        if ans[n-1]!=-1 and ans[n-2]!=-1:
            ans[n] = ans[n-1] + ans[n-2]
        elif ans[n-1]!=-1:
            ans[n] = ans[n-1] + fcci(ans,n-2)
        elif ans[n-2]!=-1:
            ans[n] = ans[n-2] + fcci(ans,n-1)
        else:
            ans[n] = fcci(ans,n-2) + fcci(ans,n-1)
        return ans[n]
    return fcci(ans,k)
  1. Bottom-up variation
def fib(k):
    if k==0: return 0
    if k==1: return 1
    ans = [-1 for i in range(k+1)]
    ans[1] = 1
    ans[0] = 0
    for i in range(2,k+1):
        ans[i] = ans[i-1] + ans[i-2]
    return ans[k]

From the code above, we can find that the process of DP also uses the recursion.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值