Files
freeCodeCamp/guide/chinese/computer-science/dynamic-programming/index.md
Ahmad Abdolsaheb 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

51 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Dynamic Programming
localeTitle: 动态编程
---
## 动态编程
动态编程DP是一种编程技术用于解决其子问题的计算重叠的问题您以避免重新计算已解决问题的方式编写程序。 这种技术通常与memoization结合使用memoization是一种优化技术可以缓存先前计算的结果并在再次需要相同的计算时返回缓存的结果。
Fibonacci系列的一个例子定义如下
`F(N) = F(N-1) + F(N-2)`
这是找到F5的树
![斐波那契系列的树](https://cdn-media-1.freecodecamp.org/imgr/59Rpw.png)
要计算F5它需要计算相同Fi的许多倍。使用递归
```python
def fib(n)
{
if n <= 1:
return n
return fib(n-1) + fib(n-2);
}
```
以下是优化的解决方案使用DP
对于F5该解决方案将生成上图中描绘的调用在O2 ^ N中运行。
这是一个使用DP和memoization的优化解决方案
```python
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]
}
```
在查找表中缓存计算解决方案并在递归之前查询它将使程序具有ON的运行时间。
#### 更多信息:
[什么是StackOverflow上的动态编程](https://stackoverflow.com/questions/1065433/what-is-dynamic-programming) [StackOverflow上的memoization和DP之间的区别](https://stackoverflow.com/questions/6184869/what-is-the-difference-between-memoization-and-dynamic-programming)