freeCodeCamp/guide/chinese/computer-science/dynamic-programming/index.md

51 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Dynamic Programming
localeTitle: 动态编程
---
## 动态编程
动态编程DP是一种编程技术用于解决其子问题的计算重叠的问题您以避免重新计算已解决问题的方式编写程序。 这种技术通常与memoization结合使用memoization是一种优化技术可以缓存先前计算的结果并在再次需要相同的计算时返回缓存的结果。
Fibonacci系列的一个例子定义如下
`F(N) = F(N-1) + F(N-2)`
这是找到F5的树
![斐波那契系列的树](https://i.stack.imgur.com/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)