Skip to content

Commit fde9470

Browse files
authored
Improve grammar
1 parent ae25f8f commit fde9470

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

dynamic_programming/AnalysisOfDynamicProgramming.md

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
This article is an advanced version of our famous work [Analysis of Dynamic Programming] which gets more than 200 stars.
44

5-
By the way, our official account has at least a dozen of articles which disassemble the problem of dynamic programming, all of which I have merged into the [list of articles] , **they all mention the problem solving framework of dynamic programmingwe systematically summarize that**. During this period, I have grown from a non class of computer science to who has completed more than half of the LeetCode, so what I summarize may not be suitable for every master, but for the public, after all I hustle all the way.
5+
By the way, our official account has at least a dozen of articles which disassemble the problem of dynamic programming, all of which I have merged into the list of articles. **They all mention the problem solving framework of dynamic programming, and we systematically summarize that.** During this short period, I have grown from a person without any formal training in computer science to who has completed more than half of the LeetCode, so what I summarize may not be suitable for every master, but for the public; after all, I have worked hard to get to where I am.
66

7-
There are a few set-pieces for algorithm skills, if you know, you will get a lot easier, this articles aims to see through dynamic programming and form a framework to solve this series of problems. Let's get into the straight point, here are the full contents.
7+
There are a few set-pieces for algorithm skills. If you know these, you will get a lot easier, this article aims to see through dynamic programming and form a framework to solve this series of problems. Let's get into the straight point. Here are the full contents.
88

99
**The normal form of dynamic programming problem is to calculate the maximum or minimum**. Dynamic programming is, in fact, a method in optimization, which just has many applications in problems in computer science, such as the problem to calculate the **longest** increasing subsequence, and the problem to calculate **the smallest** distance of editing.
1010

1111
Since we are asked to calculate the maximum or minimum, what is the core problem? **The core of the problem is enumeration**. Because we are asked to calculate the max or min, we must enumerate all the feasible answers and find max or min among those.
1212

1313
Is dynamic programming so easy that only enumeration is OK? What I see about dynamic programming problems are all hard.
1414

15-
Firstly, the enumeration of dynamic programming is a bit special, because there **exists [overlapped subproblems]** this kind of problems have extremely low efficiency, we need [memos] or [DP table] to optimize the process of enumeration to avoid unnecessary calculations.
15+
Firstly, the enumeration of dynamic programming is a bit special, because there **exist "overlapped subproblems"** this kind of problems have extremely low efficiency, we need a "memos" or "DP table" to optimize the process of enumeration to avoid unnecessary calculations.
1616

17-
And that, the DP problems must **have [best substructure]** , only through the max or min of subproblems can we obtain the max or min of original problems.
17+
And that, the DP problems must **have the "best substructure"** , only through the max or min of subproblems can we obtain the max or min of original problems.
1818

19-
Moreover, although the core idea of DP is to enumerate for max or min, the problem itself varies, to enumerate all feasible answers is not a easy thing, only when listing the **correct [state transition equation]** can we enumerate perfectly.
19+
Moreover, although the core idea of DP is to enumerate for max or min, the problem itself varies, to enumerate all feasible answers is not a easy thing, only when listing the **correct "state transition equation"** can we enumerate perfectly.
2020

2121
The overlapped problems, best substructure and state transition equation are the three elements of DP. What that means will be told in detail, however, in the practical algorithm problems, **it is hardest to write out the state transition equation** , which is why many people consider DP hard, here I provide a thinking model researched on by myself, to support you to consider the state transition equation.
2222

@@ -61,7 +61,7 @@ This is the first property in DP: **overlapped subproblem.** Next, we try to sol
6161
6262
**2. recursive solution with memos**
6363
64-
To know well the problem is to solve a half. Since the reason for wasting time is repeating calculation, then we can make a [memo], every time you obtain the answer of a subproblem , record it to the [memo] and then return; every time you meet a problem of [memo] check the [memo], if you find you have solved that before, just take the answer out for use, and do not waste time to calculate.
64+
To know well the problem is to solve a half. Since the reason for wasting time is repeating calculation, then we can make a memo, every time you obtain the answer of a subproblem , record it to the memo and then return; every time you meet a problem of memo check the memo, if you find you have solved that before, just take the answer out for use, and do not waste time to calculate.
6565
6666
Generally an array is used as a memo, of course you can use hash table (dictionary), the thought is the same.
6767
@@ -85,11 +85,11 @@ int helper(vector<int>& memo, int n) {
8585
}
8686
```
8787

88-
Now, draw the recursive tree, and you will know what [memo] does.
88+
Now, draw the recursive tree, and you will know what memo does.
8989

9090
![](../pictures/动态规划详解进阶/2.jpg)
9191

92-
In fact, recursive algorithms together with [memos], put a recursive tree with abundant redundancy through [pruning], we change one recursive graph without redundancy, which intensely decreases the number of subproblems (namely the node in recursive graph)
92+
In fact, recursive algorithms together with memos, put a recursive tree with abundant redundancy through pruning, we change one recursive graph without redundancy, which intensely decreases the number of subproblems (namely the node in the recursive graph).
9393

9494
![](../pictures/动态规划详解进阶/3.jpg)
9595

@@ -101,7 +101,7 @@ The number of total subproblems, namely the total number of nodes in graph, beca
101101

102102
The time to solve a subproblem, as above, there is no loop, the time is O(1).
103103

104-
Therefore, the time complexity of this algorithm is O(n).Instead of a brute-force algorithm, it's a dimension reduction attack.
104+
Therefore, the time complexity of this algorithm is O(n). Instead of a brute-force algorithm, it's a dimension reduction attack.
105105

106106
So far, the efficiency of the recursive solution with memos is the same as that of the iterative dynamic programming solution. In fact, this approach is almost identical to iterative dynamic programming, except that it is called "top-down" and dynamic programming is called "bottom-up".
107107

@@ -132,13 +132,13 @@ Here, we introduce the term "state transition equation", which is actually the m
132132
133133
![](../pictures/动态规划详解进阶/fib.png)
134134
135-
Why is it called "state transfer equation"? To sound high-end. You want f of n to be a state n, and that state n is transferred from the sum of the states n minus 1 and n minus 2, that's called a state transfer, that's all.
135+
Why is it called the "state-transition equation"? To sound fancy. You want f of n to be a state n, and that state n is transferred from the sum of the states n minus 1 and n minus 2, that's called a state transfer that's all.
136136
137-
You will find that all the operations in the above solutions, such as return f(n - 1) + f(n - 2), dp[i] = dp[i - 1] + dp[i - 2], and the initialization of the memo or dp table, all revolve around different representations of this equation. It is important to list the "state transfer equation", which is the core of the solution. It's easy to see that the equation of state transition directly represents the violent solution.
137+
You will find that all the operations in the above solutions, such as return f(n - 1) + f(n - 2), dp[i] = dp[i - 1] + dp[i - 2], and the initialization of the memo or dp table, all revolve around different representations of this equation. It is important to list the "state transfer equation", which is the core of the solution. It's easy to see that the equation of state transition directly represents the brute-force solution.
138138
139-
** never look down upon the violent solution, the most difficult part of the dynamic programming problem is to write the state transfer equation **, that is, the violent solution. Optimization method is nothing more than the use of memorandum or DP table, no mystery at all.
139+
**Never look down upon the brute-force solution. the most difficult part of the dynamic programming problem is to write the state transfer equation**, that is, the brute-force solution. The optimization method is nothing more than the use of the memo or DP table no mystery at all.
140140
141-
At the end of this example, there's a little detail optimization. Careful readers will find that, according to the state transition equation of the Fibonacci sequence, the current state is only related to the previous two states. In fact, it is not necessary to have a long DP table to store all the states, just find a way to store the previous two states. Therefore, it can be further optimized to reduce the space complexity to O(1) :
141+
At the end of this example, there's a little optimization detail. Observant readers will find that, according to the state transition equation of the Fibonacci sequence, the current state is only related to the previous two states. In fact, it is not necessary to have a long DP table to store all the states, just find a way to store the previous two states. Therefore, it can be further optimized to reduce the space complexity to O(1):
142142
143143
```cpp
144144
int fib(int n) {
@@ -176,7 +176,7 @@ First, the problem is dynamic programming because it has an "optimal substructur
176176

177177

178178

179-
For example, your original problem is to get the highest total score, then your sub-problem is to get the highest Chinese test, mathematics test to the highest......In order to get the highest score in each subject, you need to get the highest multiple choice score for each subject and the highest fill-in-the-blank score...Of course, the end result is that you get full marks for each course, which is the highest total score.
179+
For example, your original problem is to get the highest total score, then your sub-problem is to get the highest Chinese test, mathematics test to the highest. In order to get the highest score in each subject, you need to get the highest multiple choice score for each subject and the highest fill-in-the-blank score. Of course, the end result is that you get full marks for each course, which is the highest total score.
180180

181181

182182

@@ -192,21 +192,19 @@ Going back to the problem of making small change, why does it fit the optimal su
192192

193193

194194

195-
So, now that you know that this is a dynamic programming problem, you have to think about
195+
So, now that you know that this is a dynamic programming problem, you have to think about **how to get the right transition equation**.
196196

197-
**how do you get the right transition equation**?
198197

199198

199+
**First determine the "state"**, which is the variable that changes in the original problem and subproblems. Since the number of COINS is infinite, the only state is the target amount `amount`.
200200

201-
**first determine the "state" **, which is the variable that changes in the original problem and subproblems. Since the number of COINS is infinite, the only state is the target amount 'amount'.
202201

203202

203+
**then determine the definition of the `dp` function ** : the current target amount is `n`, at least `dp(n)` COINS are needed to make up the amount.
204204

205-
**then determine the definition of the 'dp' function ** : the current target amount is' n ', at least 'dp(n)' COINS are needed to make up the amount.
206205

207206

208-
209-
**then determine the "choice" and choose the best **, that is, for each state, what choices can be made to change the current state. Specific to this problem, no matter what the target amount is, the choice is to choose a coin from the denomination list 'COINS', and then the target amount will be reduced:
207+
**Then determine the "choice" and choose the best**, that is, for each state, what choices can be made to change the current state. Specific to this problem, no matter what the target amount is, the choice is to choose a coin from the denomination list 'COINS', and then the target amount will be reduced:
210208

211209
```python
212210
# Pseudocode framework
@@ -243,7 +241,7 @@ def coinChange(coins: List[int], amount: int):
243241
return dp(amount)
244242
```
245243

246-
So far, the state transfer equation has been completed, and the above algorithm is already a violent solution. The mathematical form of the above code is the state transfer equation:
244+
So far, the state transfer equation has been completed, and the above algorithm is already a brute-force solution. The mathematical form of the above code is the state transfer equation:
247245

248246
![](../pictures/动态规划详解进阶/coin.png)
249247

@@ -282,7 +280,7 @@ def coinChange(coins: List[int], amount: int):
282280
return dp(amount)
283281
```
284282

285-
Without drawing, it is obvious that "memorandum" greatly reduces the number of subproblems and completely eliminates the redundancy of subproblems, so that the total number of subproblems will not exceed n, that is, the number of subproblems is O(n).The time to deal with a subproblem is still O(k), so the total time complexity is O(kn).
283+
Without drawing, it is obvious that the memo greatly reduces the number of subproblems and completely eliminates the redundancy of subproblems, so that the total number of subproblems will not exceed n, that is, the number of subproblems is O(n).The time to deal with a subproblem is still O(k), so the total time complexity is O(kn).
286284

287285
**3. Iterative solution of dp array**
288286

@@ -310,7 +308,7 @@ int coinChange(vector<int>& coins, int amount) {
310308
311309
![](../pictures/动态规划详解进阶/6.jpg)
312310
313-
PS: why is the 'dp' array initialized to 'amount + 1'? Because the number of COINS that can be added to 'amount' can only be equal to 'amount' (all 1-dollar COINS) at most, initializing 'amount + 1' is the same as initializing 'amount + 1' to + infinity for subsequent minimization.
311+
PS: why is the `dp` array initialized to `amount + 1`? Because the number of COINS that can be added to `amount` can only be equal to `amount` (all one-dollar COINS) at most, initializing `amount + 1` is the same as initializing `amount + 1` to +∞ (infinity) for subsequent minimization.
314312
315313
### 3. Final conclusion
316314
@@ -319,27 +317,27 @@ The first Fibonacci sequence problem explains how to optimize a recursion tree b
319317
320318
321319
322-
The second coin problem shows how to streamline the process of determining the "state transfer equation", by which violent recursive solutions are written, the rest is to optimize the recursion tree and eliminate overlapping subproblems.
320+
The second coin problem shows how to streamline the process of determining the "state transfer equation", by which brute-force recursive solutions are written, the rest is to optimize the recursion tree and eliminate overlapping subproblems.
323321
324322
325323
326324
If you don't know much about dynamic programming, you can still see it here, and I really want to give you a hand, because I think you've mastered the design of this algorithm.
327325
328326
329327
330-
**there is nothing magical about a computer solving a problem. Its only solution is to exhaust all possibilities.** Algorithm design is nothing more than thinking "how to exhaustive" first, and then pursuing "how to exhaustive intelligently".
328+
**There is nothing magical about a computer solving a problem. Its only solution is to exhaust all possibilities.** Algorithm design is nothing more than thinking "how to be exhaustive" first, and then pursuing "how to be exhaustive intelligently".
331329
332330
333331
334-
To write the dynamic transfer equation is to solve the "how to exhaustive" problem. The reason why it is difficult is that many exhaustive operations require recursive implementation, and the second reason is that the solution space of some problems is complex and not easy to exhaustive.
332+
To write the dynamic transfer equation is to solve the "how to be exhaustive" problem. The reason why it is difficult is that many exhaustive operations require recursive implementation, and the second reason is that the solution space of some problems is complex and not easy to exhaustive.
335333
336334
337335
338-
Memo, DP table is in the pursuit of "how to intelligently exhaustive."With the idea of space for time, is the only way to reduce the complexity of time, in addition to, ask, also can play what flower work?
336+
The memo or DP table is in the pursuit of "how to intelligently be exhaustive." With the idea of space for time, is the only way to reduce the complexity of time, in addition to, ask, also can play what flower work?
339337
340338
341339
342-
**work to make the algorithm clear! Welcome to pay attention to my WeChat public number labuladong, see more easy-to-understand articles ** :
340+
**Work to make the algorithm clear! You are welcome to pay attention to my WeChat public number labuladong, see more easy-to-understand articles ** :
343341
Translator: Jian Ma
344342
345343
Author: labuladong

0 commit comments

Comments
 (0)