提交 306df0e0 编写于 作者: Y yb

translated 动态规划详解.md -> DemystifyingDynamicProgramming.md

上级 a5d00752
......@@ -29,7 +29,7 @@ This command specifies the `english` branch and limit the depth of clone, get ri
* [Longest Increasing Subsequence](dynamic_programming/LongestIncreasingSubsequence.md)
* [KMP Algorithm In Detail](dynamic_programming/KMPCharacterMatchingAlgorithmInDynamicProgramming.md)
* [House Robber Problems](dynamic_programming/HouseRobber.md)
* [动态规划详解](dynamic_programming/动态规划详解进阶.md)
* [Demystifying Dynamic Programming](dynamic_programming/DemystifyingDynamicProgramming.md)
* [团灭 LeetCode 股票买卖问题](dynamic_programming/团灭股票问题.md)
* II. Data Structure
......
......@@ -16,7 +16,7 @@
* [Longest Increasing Subsequence](dynamic_programming/LongestIncreasingSubsequence.md)
* [KMP Algorithm In Detail](dynamic_programming/KMPCharacterMatchingAlgorithmInDynamicProgramming.md)
* [House Robber Problems](dynamic_programming/HouseRobber.md)
* [动态规划详解](dynamic_programming/动态规划详解进阶.md)
* [Demystifying Dynamic Programming](dynamic_programming/DemystifyingDynamicProgramming.md)
* [团灭 LeetCode 股票买卖问题](dynamic_programming/团灭股票问题.md)
* II. Data Structure
......
# Demystifying Dynamic Programming
**Typically, all the problems that require to maximize or minimize certain quantity or counting problems that say to count the arrangements under certain condition or certain probability problems can be solved by using Dynamic Programming.**
In fact, Dynamic programming is one optimization method in Operations Research. How ever, it is widely utilized in computer applications. For examples, find **the longest** Ascending subsequence or **the minimum** edit distance, so on and so forth.
Since DP is desired for the optimal value, what will be the core issues there? Given that we need to find an optimal solution, sure thing that we will need to exhaustively list out all possible solutions.
Among that, find the best solution is our target. Hence, it's safe to say **the key of Dynamic Programming is Exhaustion**. So simple.
 
Is DP so simple? How could so many DP problems I faced are so diffuclt?
Firstly, DP has it own pattern namely **Overlapping Subproblems**.
If using a brute-force method based on exhaustive search, it will be extremely low efficient since it need a lot of computing power or a lot of time to complete.
Hence, `Memoization` or `Tabulation` can be used to accelerate this process to avoid unnecessary calculation. Before we study how to think Dynamically for a problem, we need to learn:
As said, all dynamic programming problems must satisfy the overlapping subproblems property and most of the classic dynamic problems also satisfy the **optimal substructure property**.
Once, we observe these properties in a given problem, be sure that it can be solved using DP.
Though Exhaustion is the key of DP, the problems are varies that exhaustion can not be the solution all the time.
Only the right state definition and transition you make will lead a proper solution.
Mentioned above `Overlapping Subproblems`, `Optimal Substracture property` and `state transition` are three key elements in DP.
We will explain it with detailed example in later chapters. In practical, write state transition equation is the key. That's why many people say DP is difficult. This article we proposed a thinking Frame to facilitate the state equation.
Deciding `State` -> Defining relation among states -> Formulating `DP equation` -> Finalizing base case
Here gives examples of `Fibonacci Numbers` and `Coin Change` to illustrate the basic principle of DP.
The first case demos you the overlapping subproblems while second one shows the state equation.
**These two examples may be simple but clear enough to help you to understand the thinking and beauty of DP**. If you prefer obscure problems I am sure you can find many in historical posts or online.
### Fibonacci Numbers
**1、brute-force recursive**
The following is a recursive program for Fibonacci Numbers:
```cpp
int fib(int N) {
if (N == 1 || N == 2) return 1;
return fib(N - 1) + fib(N - 2);
}
```
Needless to say, school teachers always take this example for recursion. We know this code is simple, clean and understandable.
However, it is very low efficiency. Let's say a recursion tree when `n=20`.
PS: Whenever have recursion problems, recursion tree is a better way to help you analyse the complexity of algorithm and identify the defects if any.
![](../pictures/动态规划详解进阶/1.jpg)
How to understand this recursion tree? That is to say if need to get `f(20)`, we need to calculate `f(19)` and `f(18)` first. Then we will need to get `f(19)`. Similarly, we need to get subquestions `f(18)` and `f(17)`
till we traverse to `f(1)` or `f(2)` where we can get the immediate result without any further recursion.
**How to calculate the time complexity of Recursion?Number of subquestions * the Time needed。**
We can see that the function fib(3) is being called 2 times. If we would have stored the value of fib(3), then instead of computing it again, we could have reused the old stored value. There are following two different ways to store the values so that these values can be reused:
Number of subquestions is the total number of nodes in recursion tree.
Apparently, total number of nodes is exponential that the number of subquestions is `O(2^n)`.
For a subproblem, There is no loop in this algorithm. Only one increase operation `f(n - 1) + f(n - 2)` which can be done in `O(1)`.
Hence the time complexity of this algorithm is exponential - `O(2^n)`.
If observe carefully, you will find that there are massive duplicate calculations that caused low efficiency.
i.e. the function `f(18)` is being called 2 times. You can see there are large amount of nodes in subtree under `f(18)`.
So many duplications in calculation are the reason why it is such low efficient.
It comes to the first nature of DP - **Overlapping Subproblems**. Below gives possible solutions.
**2、Memoized Recursive solution **
Defined the question is a half done of given problem. As duplicate calculation is causing long time-consuming, we can introduce a `memo` to record the results for each subproblems.
It will fetch the `memo`for a subproblem. If it has the result then directly return the result from `memo` without calculate it again.
Generally, an array can be used as 'memo'. Also you can use hash table or other data structure who has same thinking.
```cpp
int fib(int N) {
if (N < 1) return 0;
// Initialize memo to 0
vector<int> memo(N + 1, 0);
// simplest case
return helper(memo, N);
}
int helper(vector<int>& memo, int n) {
// base case
if (n == 1 || n == 2) return 1;
// calculated case
if (memo[n] != 0) return memo[n];
memo[n] = helper(memo, n - 1) +
helper(memo, n - 2);
return memo[n];
}
```
Here illustrates the recursion tree for your understanding of how `memo` works.
![](../pictures/动态规划详解进阶/2.jpg)
In fact, Memorized recursion algorithm is weeding out redundant nodes in recursion tree through `pruning` to minimize the number of nodes.
![](../pictures/动态规划详解进阶/3.jpg)
What is the time complexity of this memorized recursive algorithm?
Since no redundant calculation, the subproblems is `f(1)`, `f(2)`, `f(3)` ... `f(20)` which is liner to the input `n = 20`.
So the number of subproblems is `O(n)`.
As mentioned above, for a subproblem, time complexity is `O(1)`.
Thus, time complexity is `O(n)`. Compared to brute-force exhaustion, I am over the moon.
Given that memorized program has same efficiency with recursive DP.
In fact, there is no much difference between this two. People call Memorized one as `Top down` and `Bottom Up` for DP.
What is `Top Down`? Please note the recursion tree is in top to down manner from a big problem saying `f(20)`. Menorized program break it down till to `f(1)` and `f(2)` and return the result for each layer. This is the Top Down
What is `Bottom Up `?
In a reverse way, we can start the calculate from the bottom most problem `f(1)` and `f(2)` till we calculate the result of `f(20)`.
This is the thinking of DP who normally diff from recursion and achieved by loop iteration.
**3、dp with Tabulation**
Inspired by `Menorized` program, we can separate this `memo` into a table, let's say DP table.
Wouldn’t it be handy to build a table in bottom up fashion and return the result from last entry.
```cpp
int fib(int N) {
vector<int> dp(N + 1, 0);
// base case
dp[1] = dp[2] = 1;
for (int i = 3; i <= N; i++)
dp[i] = dp[i - 1] + dp[i - 2];
return dp[N];
}
```
![](../pictures/动态规划详解进阶/4.jpg)
You will find this DP table is quite similar to the `pruning` results but a reverse way of calculation manner. In most cases, Memorized programm and this DP table have same time complexity.
Here gives the `State transition equation` who describe the problem structure in mathematical.
![](../pictures/动态规划详解进阶/fib.png)
为啥叫「状态转移方程」?为了听起来高端。你把 f(n) 想做一个状态 n,这个状态 n 是由状态 n - 1 和状态 n - 2 相加转移而来,这就叫状态转移,仅此而已。
Why call it `state transition equation`?
Let’s think f(n) as state n. The state n is obtained by state n - 1 plus state n - 2, so called state transition equation.
You will find above solutions embody this state equation in various forms.
For example, return f(n - 1) + f(n - 2), dp[i] = dp[i - 1] + dp[i - 2] and memorized or DP table operations.
No need to say, the state equation is the key in solving DP problems. In fact, state equation represnts the brute-force exhaustion.
**Don't look down the brute-force exhaustion. The most difficult part of DP is define the state equation.**,Optimized by memorization or DP table, exhaustion can achieve a good performance in most case.
If look carefully, you will notice that the Fibonacci Numbers only depend on the previous two states. It is not necessary to have a big DP table to store all states. Only store latest two states will significantly improve the efficiency and recude the space complexity to O(1).
```cpp
int fib(int n) {
if (n == 2 || n == 1)
return 1;
int prev = 1, curr = 1;
for (int i = 3; i <= n; i++) {
int sum = prev + curr;
prev = curr;
curr = sum;
}
return curr;
}
```
Someone may ask where is the optimal substructure? We will cover it in below sections.
Strictly speaking, Fibonacci Numbers is not a typically DP problem as it does not have a optimal value. Those examples demonstrate the algorithm design with top-down and bottom-up processes.
Let's look at the second example - `coin change`.
### Coin Change Problem
There are infinite supply of `k` valued coins with `c1, c2 ... ck` respectively. Given a value `amount`, what is the minimum number of coins to get the given amount? If can not get the amount, return `-1`. Method signature is as below.
```java
// coins - valued coins to be used ,amount is the target value
int coinChange(int[] coins, int amount);
```
For example, `k = 3` with coin values `1, 2, 5`, to get `amount = 11` at least 3 coins are needed as `11 = 5 + 5 + 1`.
What do you think how will computer solve this problem? Apparently, it will exhaustively list all possible solutions and find the minimum number of coins.
**1、brute-force recursion**
It can be solve with DP since it has the `Optimal Substructure`. **To meet the「Optimal Substructure」, the subprobles must be independent**.
Why we say this coin change problem satisfies the optimal substructure?
Image that to get the minimum coins for `amount = 11`, if you can get the answer for `amount = 10`. Then you merely add the answer by 1. Cause there is infinite supply and those subproblems are independent.
Since it is DP problem, we will need to define a right `state transition equation`.
**Define the `state`**,the variables in original problem ans subproblem。Due to infinite supply, the only target state the `amount`
**Formulate `dp` equation**:For current amount `n`,at least need `dp(n)` coins。
**Decide selection and optimize**,For each state, it decides how to transit the states. Specify to this problem, whatever amount it is, it will choose one coin from `coins` and decrease the target amount.
```python
# pseudo-code
def coinChange(coins: List[int], amount: int):
# define:to get amount - n,at least need dp(n) number of cpoins
def dp(n):
# do selection,choose the minimum results
for coin in coins:
res = min(res, 1 + dp(n - coin))
return res
# target answer - dp(amount)
return dp(amount)
```
**lastly finalize base case**,if target amount is 0,required coins is 0;when target amount less than 0, return -1:
```python
def coinChange(coins: List[int], amount: int):
def dp(n):
# base case
if n == 0: return 0
if n < 0: return -1
# to get minimum value,set initial value to infinite
res = float('INF')
for coin in coins:
subproblem = dp(n - coin)
# if no solution for subproblem,skip it
if subproblem == -1: continue
res = min(res, 1 + subproblem)
return res if res != float('INF') else -1
return dp(amount)
```
Till now, state transition equation on basis of brute-force is completed. Above pseudo-code shows the state equation.
![](../pictures/动态规划详解进阶/coin.png)
To further eliminate the duplicated subproblems, we give the recursion tree. i.e. `amount = 11, coins = {1,2,5}`
![](../pictures/动态规划详解进阶/5.jpg)
**Time Complexity Analysis:NO. subproblems x Time of subproblem**
NO. subproblems is exponential - O(n^k). There is a for loop in each subproblem that the time complexity is O(k). Hence total time complexity is also exponential - O(k * n^k).
**2、Memorized rercusion**
With a little modification, it can be converted to a memorization solution.
```python
def coinChange(coins: List[int], amount: int):
# Memorization
memo = dict()
def dp(n):
# lookup memo to avoid duplication
if n in memo: return memo[n]
if n == 0: return 0
if n < 0: return -1
res = float('INF')
for coin in coins:
subproblem = dp(n - coin)
if subproblem == -1: continue
res = min(res, 1 + subproblem)
# record to Memo
memo[n] = res if res != float('INF') else -1
return memo[n]
return dp(amount)
```
Memorized solution completed eliminate the overlaps that number of subproblesm is decreased to O(n). The time complexity for subproblem is still `O(K)`. So total time complexity becomes O(k * n^k).
**3、dp 数组的迭代解法**
Definitely, we can apply the dp table a bottom-up based solution to avoid the overlaps. Same to previous definition of `dp` functions.
**`dp[i] = x` denotes when target amount is `i`,at least requires `x` coins**
```cpp
int coinChange(vector<int>& coins, int amount) {
// array size is amount + 1,initial value is amount + 1
vector<int> dp(amount + 1, amount + 1);
// base case
dp[0] = 0;
for (int i = 0; i < dp.size(); i++) {
// inner for loop to get minimum value for all subproblems + 1
for (int coin : coins) {
// Skip if no solution
if (i - coin < 0) continue;
dp[i] = min(dp[i], 1 + dp[i - coin]);
}
}
return (dp[amount] == amount + 1) ? -1 : dp[amount];
}
```
![](../pictures/动态规划详解进阶/6.jpg)
PS: Why initialize `dp` array with value `amount + 1` cause that at most `amount` number of coins (valued 1) to make value `amount`.
Hence initial value `amount + 1`is equivalent to infinity for this problem and make it easier to get the minimum value.
### Summary
Start with the Fibonacci Numbers, we explained two solutions of memorized and dp table to optimize the recursion tree. These two solutions have same nature but different approach from top-down and bottom-up.
The second coin change problem illustrated the how to define the `state transition expresion` for brute-force sulution with optimized recursion tree to eliminate the overlaps.
Big applause to you if reach here though you are not familiar with DP. I believe you have mastered the skills for DP problems.
**No siver bullet in computer world. Exhaustion is the only key.** Algorithm design does nothing but find a smart way for Exhaustion.
Formulating State equation is the way to solve the brute-force exhaustion problem.
This is difficult cause exhaustion will require recursion in implementation and secondly some problems are complex themselves to be exhaustively resolved.
Memorization and DP table are the smart solutions to exhaustion.
The idea that sacrificing space to improve efficiency is the only way to reduce the time complexity. Isn't it?
**Make algorithm clear!Welcome to follow Wechat official account `labuladong` to obtain more comprehensive articles**
![labuladong](../pictures/labuladong.png)
\ No newline at end of file
# 动态规划详解
这篇文章是我们号半年前一篇 200 多赞赏的成名之作「动态规划详解」的进阶版。由于账号迁移的原因,旧文无法被搜索到,所以我润色了本文,并添加了更多干货内容,希望本文成为解决动态规划的一部「指导方针」。
再说句题外话,我们的公众号开号至今写了起码十几篇文章拆解动态规划问题,我都整理到了公众号菜单的「文章目录」中,**它们都提到了动态规划的解题框架思维,本文就系统总结一下**。这段时间本人也从非科班小白成长到刷通半个 LeetCode,所以我总结的套路可能不适合各路大神,但是应该适合大众,毕竟我自己也是一路摸爬滚打过来的。
算法技巧就那几个套路,如果你心里有数,就会轻松很多,本文就来扒一扒动态规划的裤子,形成一套解决这类问题的思维框架。废话不多说了,上干货。
**动态规划问题的一般形式就是求最值**。动态规划其实是运筹学的一种最优化方法,只不过在计算机问题上应用比较多,比如说让你求**最长**递增子序列呀,**最小**编辑距离呀等等。
既然是要求最值,核心问题是什么呢?**求解动态规划的核心问题是穷举**。因为要求最值,肯定要把所有可行的答案穷举出来,然后在其中找最值呗。
动态规划就这么简单,就是穷举就完事了?我看到的动态规划问题都很难啊!
首先,动态规划的穷举有点特别,因为这类问题**存在「重叠子问题」**,如果暴力穷举的话效率会极其低下,所以需要「备忘录」或者「DP table」来优化穷举过程,避免不必要的计算。
而且,动态规划问题一定会**具备「最优子结构」**,才能通过子问题的最值得到原问题的最值。
另外,虽然动态规划的核心思想就是穷举求最值,但是问题可以千变万化,穷举所有可行解其实并不是一件容易的事,只有列出**正确的「状态转移方程**」才能正确地穷举。
以上提到的重叠子问题、最优子结构、状态转移方程就是动态规划三要素。具体什么意思等会会举例详解,但是在实际的算法问题中,**写出状态转移方程是最困难的**,这也就是为什么很多朋友觉得动态规划问题困难的原因,我来提供我研究出来的一个思维框架,辅助你思考状态转移方程:
明确「状态」 -> 定义 dp 数组/函数的含义 -> 明确「选择」-> 明确 base case。
下面通过斐波那契数列问题和凑零钱问题来详解动态规划的基本原理。前者主要是让你明白什么是重叠子问题(斐波那契数列严格来说不是动态规划问题),后者主要举集中于如何列出状态转移方程。
请读者不要嫌弃这个例子简单,**只有简单的例子才能让你把精力充分集中在算法背后的通用思想和技巧上,而不会被那些隐晦的细节问题搞的莫名其妙**。想要困难的例子,历史文章里有的是。
### 一、斐波那契数列
**1、暴力递归**
斐波那契数列的数学形式就是递归的,写成代码就是这样:
```cpp
int fib(int N) {
if (N == 1 || N == 2) return 1;
return fib(N - 1) + fib(N - 2);
}
```
这个不用多说了,学校老师讲递归的时候似乎都是拿这个举例。我们也知道这样写代码虽然简洁易懂,但是十分低效,低效在哪里?假设 n = 20,请画出递归树。
PS:但凡遇到需要递归的问题,最好都画出递归树,这对你分析算法的复杂度,寻找算法低效的原因都有巨大帮助。
![](../pictures/动态规划详解进阶/1.jpg)
这个递归树怎么理解?就是说想要计算原问题 `f(20)`,我就得先计算出子问题 `f(19)``f(18)`,然后要计算 `f(19)`,我就要先算出子问题 `f(18)``f(17)`,以此类推。最后遇到 `f(1)` 或者 `f(2)` 的时候,结果已知,就能直接返回结果,递归树不再向下生长了。
**递归算法的时间复杂度怎么计算?子问题个数乘以解决一个子问题需要的时间。**
子问题个数,即递归树中节点的总数。显然二叉树节点总数为指数级别,所以子问题个数为 O(2^n)。
解决一个子问题的时间,在本算法中,没有循环,只有 f(n - 1) + f(n - 2) 一个加法操作,时间为 O(1)。
所以,这个算法的时间复杂度为 O(2^n),指数级别,爆炸。
观察递归树,很明显发现了算法低效的原因:存在大量重复计算,比如 `f(18)` 被计算了两次,而且你可以看到,以 `f(18)` 为根的这个递归树体量巨大,多算一遍,会耗费巨大的时间。更何况,还不止 `f(18)` 这一个节点被重复计算,所以这个算法及其低效。
这就是动态规划问题的第一个性质:**重叠子问题**。下面,我们想办法解决这个问题。
**2、带备忘录的递归解法**
明确了问题,其实就已经把问题解决了一半。即然耗时的原因是重复计算,那么我们可以造一个「备忘录」,每次算出某个子问题的答案后别急着返回,先记到「备忘录」里再返回;每次遇到一个子问题先去「备忘录」里查一查,如果发现之前已经解决过这个问题了,直接把答案拿出来用,不要再耗时去计算了。
一般使用一个数组充当这个「备忘录」,当然你也可以使用哈希表(字典),思想都是一样的。
```cpp
int fib(int N) {
if (N < 1) return 0;
// 备忘录全初始化为 0
vector<int> memo(N + 1, 0);
// 初始化最简情况
return helper(memo, N);
}
int helper(vector<int>& memo, int n) {
// base case
if (n == 1 || n == 2) return 1;
// 已经计算过
if (memo[n] != 0) return memo[n];
memo[n] = helper(memo, n - 1) +
helper(memo, n - 2);
return memo[n];
}
```
现在,画出递归树,你就知道「备忘录」到底做了什么。
![](../pictures/动态规划详解进阶/2.jpg)
实际上,带「备忘录」的递归算法,把一棵存在巨量冗余的递归树通过「剪枝」,改造成了一幅不存在冗余的递归图,极大减少了子问题(即递归图中节点)的个数。
![](../pictures/动态规划详解进阶/3.jpg)
递归算法的时间复杂度怎么算?子问题个数乘以解决一个子问题需要的时间。
子问题个数,即图中节点的总数,由于本算法不存在冗余计算,子问题就是 `f(1)`, `f(2)`, `f(3)` ... `f(20)`,数量和输入规模 n = 20 成正比,所以子问题个数为 O(n)。
解决一个子问题的时间,同上,没有什么循环,时间为 O(1)。
所以,本算法的时间复杂度是 O(n)。比起暴力算法,是降维打击。
至此,带备忘录的递归解法的效率已经和迭代的动态规划解法一样了。实际上,这种解法和迭代的动态规划已经差不多了,只不过这种方法叫做「自顶向下」,动态规划叫做「自底向上」。
啥叫「自顶向下」?注意我们刚才画的递归树(或者说图),是从上向下延伸,都是从一个规模较大的原问题比如说 `f(20)`,向下逐渐分解规模,直到 `f(1)``f(2)` 触底,然后逐层返回答案,这就叫「自顶向下」。
啥叫「自底向上」?反过来,我们直接从最底下,最简单,问题规模最小的 `f(1)``f(2)` 开始往上推,直到推到我们想要的答案 `f(20)`,这就是动态规划的思路,这也是为什么动态规划一般都脱离了递归,而是由循环迭代完成计算。
**3、dp 数组的迭代解法**
有了上一步「备忘录」的启发,我们可以把这个「备忘录」独立出来成为一张表,就叫做 DP table 吧,在这张表上完成「自底向上」的推算岂不美哉!
```cpp
int fib(int N) {
vector<int> dp(N + 1, 0);
// base case
dp[1] = dp[2] = 1;
for (int i = 3; i <= N; i++)
dp[i] = dp[i - 1] + dp[i - 2];
return dp[N];
}
```
![](../pictures/动态规划详解进阶/4.jpg)
画个图就很好理解了,而且你发现这个 DP table 特别像之前那个「剪枝」后的结果,只是反过来算而已。实际上,带备忘录的递归解法中的「备忘录」,最终完成后就是这个 DP table,所以说这两种解法其实是差不多的,大部分情况下,效率也基本相同。
这里,引出「状态转移方程」这个名词,实际上就是描述问题结构的数学形式:
![](../pictures/动态规划详解进阶/fib.png)
为啥叫「状态转移方程」?为了听起来高端。你把 f(n) 想做一个状态 n,这个状态 n 是由状态 n - 1 和状态 n - 2 相加转移而来,这就叫状态转移,仅此而已。
你会发现,上面的几种解法中的所有操作,例如 return f(n - 1) + f(n - 2),dp[i] = dp[i - 1] + dp[i - 2],以及对备忘录或 DP table 的初始化操作,都是围绕这个方程式的不同表现形式。可见列出「状态转移方程」的重要性,它是解决问题的核心。很容易发现,其实状态转移方程直接代表着暴力解法。
**千万不要看不起暴力解,动态规划问题最困难的就是写出状态转移方程**,即这个暴力解。优化方法无非是用备忘录或者 DP table,再无奥妙可言。
这个例子的最后,讲一个细节优化。细心的读者会发现,根据斐波那契数列的状态转移方程,当前状态只和之前的两个状态有关,其实并不需要那么长的一个 DP table 来存储所有的状态,只要想办法存储之前的两个状态就行了。所以,可以进一步优化,把空间复杂度降为 O(1):
```cpp
int fib(int n) {
if (n == 2 || n == 1)
return 1;
int prev = 1, curr = 1;
for (int i = 3; i <= n; i++) {
int sum = prev + curr;
prev = curr;
curr = sum;
}
return curr;
}
```
有人会问,动态规划的另一个重要特性「最优子结构」,怎么没有涉及?下面会涉及。斐波那契数列的例子严格来说不算动态规划,因为没有涉及求最值,以上旨在演示算法设计螺旋上升的过程。下面,看第二个例子,凑零钱问题。
### 二、凑零钱问题
先看下题目:给你 `k` 种面值的硬币,面值分别为 `c1, c2 ... ck`,每种硬币的数量无限,再给一个总金额 `amount`,问你**最少**需要几枚硬币凑出这个金额,如果不可能凑出,算法返回 -1 。算法的函数签名如下:
```java
// coins 中是可选硬币面值,amount 是目标金额
int coinChange(int[] coins, int amount);
```
比如说 `k = 3`,面值分别为 1,2,5,总金额 `amount = 11`。那么最少需要 3 枚硬币凑出,即 11 = 5 + 5 + 1。
你认为计算机应该如何解决这个问题?显然,就是把所有肯能的凑硬币方法都穷举出来,然后找找看最少需要多少枚硬币。
**1、暴力递归**
首先,这个问题是动态规划问题,因为它具有「最优子结构」的。**要符合「最优子结构」,子问题间必须互相独立**。啥叫相互独立?你肯定不想看数学证明,我用一个直观的例子来讲解。
比如说,你的原问题是考出最高的总成绩,那么你的子问题就是要把语文考到最高,数学考到最高…… 为了每门课考到最高,你要把每门课相应的选择题分数拿到最高,填空题分数拿到最高…… 当然,最终就是你每门课都是满分,这就是最高的总成绩。
得到了正确的结果:最高的总成绩就是总分。因为这个过程符合最优子结构,“每门科目考到最高”这些子问题是互相独立,互不干扰的。
但是,如果加一个条件:你的语文成绩和数学成绩会互相制约,此消彼长。这样的话,显然你能考到的最高总成绩就达不到总分了,按刚才那个思路就会得到错误的结果。因为子问题并不独立,语文数学成绩无法同时最优,所以最优子结构被破坏。
回到凑零钱问题,为什么说它符合最优子结构呢?比如你想求 `amount = 11` 时的最少硬币数(原问题),如果你知道凑出 `amount = 10` 的最少硬币数(子问题),你只需要把子问题的答案加一(再选一枚面值为 1 的硬币)就是原问题的答案,因为硬币的数量是没有限制的,子问题之间没有相互制,是互相独立的。
那么,既然知道了这是个动态规划问题,就要思考**如何列出正确的状态转移方程**
**先确定「状态」**,也就是原问题和子问题中变化的变量。由于硬币数量无限,所以唯一的状态就是目标金额 `amount`
**然后确定 `dp` 函数的定义**:当前的目标金额是 `n`,至少需要 `dp(n)` 个硬币凑出该金额。
**然后确定「选择」并择优**,也就是对于每个状态,可以做出什么选择改变当前状态。具体到这个问题,无论当的目标金额是多少,选择就是从面额列表 `coins` 中选择一个硬币,然后目标金额就会减少:
```python
# 伪码框架
def coinChange(coins: List[int], amount: int):
# 定义:要凑出金额 n,至少要 dp(n) 个硬币
def dp(n):
# 做选择,选择需要硬币最少的那个结果
for coin in coins:
res = min(res, 1 + dp(n - coin))
return res
# 我们要求的问题是 dp(amount)
return dp(amount)
```
**最后明确 base case**,显然目标金额为 0 时,所需硬币数量为 0;当目标金额小于 0 时,无解,返回 -1:
```python
def coinChange(coins: List[int], amount: int):
def dp(n):
# base case
if n == 0: return 0
if n < 0: return -1
# 求最小值,所以初始化为正无穷
res = float('INF')
for coin in coins:
subproblem = dp(n - coin)
# 子问题无解,跳过
if subproblem == -1: continue
res = min(res, 1 + subproblem)
return res if res != float('INF') else -1
return dp(amount)
```
至此,状态转移方程其实已经完成了,以上算法已经是暴力解法了,以上代码的数学形式就是状态转移方程:
![](../pictures/动态规划详解进阶/coin.png)
至此,这个问题其实就解决了,只不过需要消除一下重叠子问题,比如 `amount = 11, coins = {1,2,5}` 时画出递归树看看:
![](../pictures/动态规划详解进阶/5.jpg)
**时间复杂度分析:子问题总数 x 每个子问题的时间**
子问题总数为递归树节点个数,这个比较难看出来,是 O(n^k),总之是指数级别的。每个子问题中含有一个 for 循环,复杂度为 O(k)。所以总时间复杂度为 O(k * n^k),指数级别。
**2、带备忘录的递归**
只需要稍加修改,就可以通过备忘录消除子问题:
```python
def coinChange(coins: List[int], amount: int):
# 备忘录
memo = dict()
def dp(n):
# 查备忘录,避免重复计算
if n in memo: return memo[n]
if n == 0: return 0
if n < 0: return -1
res = float('INF')
for coin in coins:
subproblem = dp(n - coin)
if subproblem == -1: continue
res = min(res, 1 + subproblem)
# 记入备忘录
memo[n] = res if res != float('INF') else -1
return memo[n]
return dp(amount)
```
不画图了,很显然「备忘录」大大减小了子问题数目,完全消除了子问题的冗余,所以子问题总数不会超过金额数 n,即子问题数目为 O(n)。处理一个子问题的时间不变,仍是 O(k),所以总的时间复杂度是 O(kn)。
**3、dp 数组的迭代解法**
当然,我们也可以自底向上使用 dp table 来消除重叠子问题,`dp` 数组的定义和刚才 `dp` 函数类似,定义也是一样的:
**`dp[i] = x` 表示,当目标金额为 `i` 时,至少需要 `x` 枚硬币**
```cpp
int coinChange(vector<int>& coins, int amount) {
// 数组大小为 amount + 1,初始值也为 amount + 1
vector<int> dp(amount + 1, amount + 1);
// base case
dp[0] = 0;
for (int i = 0; i < dp.size(); i++) {
// 内层 for 在求所有子问题 + 1 的最小值
for (int coin : coins) {
// 子问题无解,跳过
if (i - coin < 0) continue;
dp[i] = min(dp[i], 1 + dp[i - coin]);
}
}
return (dp[amount] == amount + 1) ? -1 : dp[amount];
}
```
![](../pictures/动态规划详解进阶/6.jpg)
PS:为啥 `dp` 数组初始化为 `amount + 1` 呢,因为凑成 `amount` 金额的硬币数最多只可能等于 `amount`(全用 1 元面值的硬币),所以初始化为 `amount + 1` 就相当于初始化为正无穷,便于后续取最小值。
### 三、最后总结
第一个斐波那契数列的问题,解释了如何通过「备忘录」或者「dp table」的方法来优化递归树,并且明确了这两种方法本质上是一样的,只是自顶向下和自底向上的不同而已。
第二个凑零钱的问题,展示了如何流程化确定「状态转移方程」,只要通过状态转移方程写出暴力递归解,剩下的也就是优化递归树,消除重叠子问题而已。
如果你不太了解动态规划,还能看到这里,真得给你鼓掌,相信你已经掌握了这个算法的设计技巧。
**计算机解决问题其实没有任何奇技淫巧,它唯一的解决办法就是穷举**,穷举所有可能性。算法设计无非就是先思考“如何穷举”,然后再追求“如何聪明地穷举”。
列出动态转移方程,就是在解决“如何穷举”的问题。之所以说它难,一是因为很多穷举需要递归实现,二是因为有的问题本身的解空间复杂,不那么容易穷举完整。
备忘录、DP table 就是在追求“如何聪明地穷举”。用空间换时间的思路,是降低时间复杂度的不二法门,除此之外,试问,还能玩出啥花活?
**致力于把算法讲清楚!欢迎关注我的微信公众号 labuladong,查看更多通俗易懂的文章**
![labuladong](../pictures/labuladong.png)
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册