Files
freeCodeCamp/guide/chinese/computer-science/dynamic-programming/index.md
T
Ahmad AbdolsahebandRandell Dawson 23ca5d9cc6 fix: replace imgur with s3 for Chinese guide without conflict (#36052)
* fix: imgur to s3 for chinese guide without conflict

(cherry picked from commit 21e3afaee0f23d700f76ea662bc193b392fc54ac)

* fix: remove extra links

Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>

* fix: revert changes

* fix: revert changes

* fix: revert changes

* fix: revert changes
2019-05-25 02:39:49 +05:30

1.7 KiB
Raw Blame History

title, localeTitle
title localeTitle
Dynamic Programming 动态编程

动态编程

动态编程(DP)是一种编程技术,用于解决其子问题的计算重叠的问题:您以避免重新计算已解决问题的方式编写程序。 这种技术通常与memoization结合使用,memoization是一种优化技术,可以缓存先前计算的结果,并在再次需要相同的计算时返回缓存的结果。

Fibonacci系列的一个例子,定义如下:

F(N) = F(N-1) + F(N-2)

这是找到F(5)的树:

斐波那契系列的树

要计算F(5),它需要计算相同F(i)的许多倍。使用递归:

def fib(n) 
 { 
    if n <= 1: 
        return n 
    return fib(n-1) + fib(n-2); 
 } 

以下是优化的解决方案(使用DP)

对于F(5),该解决方案将生成上图中描绘的调用,在O(2 ^ N)中运行。

这是一个使用DP和memoization的优化解决方案:

lookup = {1 : 1, 2 : 1} # Create a lookup-table (a map) inizialized with the first 2 Fibonacci's numbers 
 
 def fib(n) 
 { 
    if n in lookup: # If n is already computed 
        return n # Return the previous computed solution 
    else 
        lookup[n] = fib(n-1) + fib(n-2) # Else, do the recursion. 
    return lookup[n] 
 } 

在查找表中缓存计算解决方案,并在递归之前查询它将使程序具有O(N)的运行时间。

更多信息:

什么是StackOverflow上的动态编程 StackOverflow上的memoization和DP之间的区别