You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/1-1000/392-is-subsequence.md
+37
Original file line number
Diff line number
Diff line change
@@ -236,6 +236,43 @@ end
236
236
- In this question, can we use a one-dimensional rolling array to implement the "dynamic programming" algorithm?
237
237
<details><summary>Click to view the answer</summary><p> Of course, but considering that a one-dimensional rolling array is not as easy to understand as a two-dimensional array, and the implementation process is also prone to errors, so here I didn't give the relevant code implementation. If you are interested, you can try it. </p></details>
238
238
239
+
## Pattern of "Dynamic Programming"
240
+
241
+
`Dynamic programming` requires the use of the `dp` array to store the results. The value of `dp[i][j]` can be derived from the value of the previous `dp[x][y]` related to it.
242
+
243
+
#### "Dynamic programming" is divided into five steps
244
+
245
+
1. Determine the meaning of each value of the array `dp`.
246
+
2. Initialize the value of the array `dp`.
247
+
3. Fill in the `dp` grid data "in order" according to an example.
248
+
4. Based on the `dp` grid data, derive the "recursive formula".
249
+
5. Write a program and print the `dp` array. If it is not as expected, adjust it.
250
+
251
+
#### Detailed description of these five steps
252
+
253
+
1. Determine the meaning of each value of the array `dp`.
254
+
- First determine whether `dp` is a one-dimensional array or a two-dimensional array. A `one-dimensional rolling array` means that the values of the array are overwritten at each iteration. Most of the time, using `one-dimensional rolling array` instead of `two-dimensional array` can simplify the code; but for some problems, such as operating "two swappable arrays", for the sake of ease of understanding, it is better to use `two-dimensional array`.
255
+
- Try to use the meaning of the `return value` required by the problem as the meaning of `dp[i]` (one-dimensional) or `dp[i][j]` (two-dimensional). It works about 60% of the time. If it doesn't work, try other meanings.
256
+
- Try to save more information in the design. Repeated information only needs to be saved once in a `dp[i]`.
257
+
- Use simplified meanings. If the problem can be solved with `boolean value`, don't use `numeric value`.
258
+
2. Initialize the value of the array `dp`. The value of `dp` involves two levels:
259
+
1. The length of `dp`. Usually: `condition array length plus 1` or `condition array length`.
260
+
2. The value of `dp[i]` or `dp[i][j]`. `dp[0]` or `dp[0][0]` sometimes requires special treatment.
261
+
3. Fill in the `dp` grid data "in order" according to an example.
262
+
- The "recursive formula" is the core of the "dynamic programming" algorithm. But the "recursive formula" is obscure. If you want to get it, you need to make a table and use data to inspire yourself.
263
+
- If the original example is not good enough, you need to redesign one yourself.
264
+
- According to the example, fill in the `dp` grid data "in order", which is very important because it determines the traversal order of the code.
265
+
- Most of the time, from left to right, from top to bottom. But sometimes it is necessary to traverse from right to left, from bottom to top, from the middle to the right (or left), such as the "palindrome" problems. Sometimes, it is necessary to traverse a line twice, first forward and then backward.
266
+
- When the order is determined correctly, the starting point is determined. Starting from the starting point, fill in the `dp` grid data "in order". This order is also the order in which the program processes.
267
+
- In this process, you will get inspiration to write a "recursive formula". If you can already derive the formula, you do not need to complete the grid.
268
+
4. Based on the `dp` grid data, derive the "recursive formula".
269
+
- There are three special positions to pay attention to: `dp[i - 1][j - 1]`, `dp[i - 1][j]` and `dp[i][j - 1]`, the current `dp[i][j]` often depends on them.
270
+
- When operating "two swappable arrays", due to symmetry, we may need to use `dp[i - 1][j]` and `dp[i][j - 1]` at the same time.
271
+
5. Write a program and print the `dp` array. If it is not as expected, adjust it.
272
+
- Focus on analyzing those values that are not as expected.
273
+
274
+
After reading the above, do you feel that "dynamic programming" is not that difficult? Try to solve this problem. 🤗
275
+
239
276
## Steps
240
277
241
278
It is a question of **comparing two strings**. After doing similar questions many times, we will develop an intuition to use `dynamic programming` with two-dimensional arrays.
Copy file name to clipboardExpand all lines: en/1-1000/416-partition-equal-subset-sum.md
+109
Original file line number
Diff line number
Diff line change
@@ -40,6 +40,78 @@ Given an integer array `nums`, return `true` if you can partition the array into
40
40
- When we first see this problem, we might want to loop through all subsets of the array. If there is a subset whose sum is equal to `half of the sum`, then return `true`. This can be achieved with a `backtracking algorithm`, but after seeing the constraint `nums.length <= 200`, we can estimate that the program will time out.
41
41
- This problem can be solved using the `0/1 knapsack problem` algorithm.
42
42
43
+
## Pattern of "Dynamic Programming"
44
+
45
+
`Dynamic programming` requires the use of the `dp` array to store the results. The value of `dp[i][j]` can be derived from the value of the previous `dp[x][y]` related to it.
46
+
47
+
#### "Dynamic programming" is divided into five steps
48
+
49
+
1. Determine the meaning of each value of the array `dp`.
50
+
2. Initialize the value of the array `dp`.
51
+
3. Fill in the `dp` grid data "in order" according to an example.
52
+
4. Based on the `dp` grid data, derive the "recursive formula".
53
+
5. Write a program and print the `dp` array. If it is not as expected, adjust it.
54
+
55
+
#### Detailed description of these five steps
56
+
57
+
1. Determine the meaning of each value of the array `dp`.
58
+
- First determine whether `dp` is a one-dimensional array or a two-dimensional array. A `one-dimensional rolling array` means that the values of the array are overwritten at each iteration. Most of the time, using `one-dimensional rolling array` instead of `two-dimensional array` can simplify the code; but for some problems, such as operating "two swappable arrays", for the sake of ease of understanding, it is better to use `two-dimensional array`.
59
+
- Try to use the meaning of the `return value` required by the problem as the meaning of `dp[i]` (one-dimensional) or `dp[i][j]` (two-dimensional). It works about 60% of the time. If it doesn't work, try other meanings.
60
+
- Try to save more information in the design. Repeated information only needs to be saved once in a `dp[i]`.
61
+
- Use simplified meanings. If the problem can be solved with `boolean value`, don't use `numeric value`.
62
+
2. Initialize the value of the array `dp`. The value of `dp` involves two levels:
63
+
1. The length of `dp`. Usually: `condition array length plus 1` or `condition array length`.
64
+
2. The value of `dp[i]` or `dp[i][j]`. `dp[0]` or `dp[0][0]` sometimes requires special treatment.
65
+
3. Fill in the `dp` grid data "in order" according to an example.
66
+
- The "recursive formula" is the core of the "dynamic programming" algorithm. But the "recursive formula" is obscure. If you want to get it, you need to make a table and use data to inspire yourself.
67
+
- If the original example is not good enough, you need to redesign one yourself.
68
+
- According to the example, fill in the `dp` grid data "in order", which is very important because it determines the traversal order of the code.
69
+
- Most of the time, from left to right, from top to bottom. But sometimes it is necessary to traverse from right to left, from bottom to top, from the middle to the right (or left), such as the "palindrome" problems. Sometimes, it is necessary to traverse a line twice, first forward and then backward.
70
+
- When the order is determined correctly, the starting point is determined. Starting from the starting point, fill in the `dp` grid data "in order". This order is also the order in which the program processes.
71
+
- In this process, you will get inspiration to write a "recursive formula". If you can already derive the formula, you do not need to complete the grid.
72
+
4. Based on the `dp` grid data, derive the "recursive formula".
73
+
- There are three special positions to pay attention to: `dp[i - 1][j - 1]`, `dp[i - 1][j]` and `dp[i][j - 1]`, the current `dp[i][j]` often depends on them.
74
+
- When operating "two swappable arrays", due to symmetry, we may need to use `dp[i - 1][j]` and `dp[i][j - 1]` at the same time.
75
+
5. Write a program and print the `dp` array. If it is not as expected, adjust it.
76
+
- Focus on analyzing those values that are not as expected.
77
+
78
+
After reading the above, do you feel that "dynamic programming" is not that difficult? Try to solve this problem. 🤗
79
+
80
+
## Pattern of "0/1 Knapsack Problem"
81
+
82
+
The typical "0/1 knapsack problem" means that each "item" can only be used once to fill the "knapsack". "Items" have "weight" and "value" attributes. Find the maximum value of "items" that can be stored in the "knapsack".
83
+
84
+
Its characteristics are: there is a **set of numbers**, each number can only be used once, and through some calculation, **another number** is obtained. The question can also be turned into whether it can be obtained? How many variations are there? And so on.
85
+
86
+
Because "0/1 Knapsack Problem" belongs to "Dynamic Programming", I will explain it in the pattern of "Dynamic Programming".
87
+
88
+
1. Determine what each value of the array `dp` represents.
89
+
- Prefer **one-dimensional rolling array** because the code is concise.
90
+
- Determine what is "item" and what is "knapsack".
91
+
- If `dp[j]` is a boolean value, then `dp[j]` indicates whether the `sum` of the first `i` items can get `j`.
92
+
- If `dp[j]` is a numerical value, then `dp[j]` indicates the maximum (or minimum) value that `dp[j]` can reach using the first `i` items.
93
+
94
+
2. Initialize the value of the array `dp`.
95
+
- Determine the size of the "knapsack". It is necessary to add 1 to the size of the knapsack, that is, insert `dp[0]` as the starting point, which is convenient for understanding and reference.
96
+
-`dp[0]` sometimes needs special treatment.
97
+
3. According to an example, fill in the `dp` grid data "in order".
98
+
- First in the outer loop, **traverse the items**.
99
+
- Then in the inner loop, **traverse the knapsack size**.
100
+
- When traversing the knapsack size, since `dp[j]` depends on `dp[j]` and `dp[j - weights[i]]`, we should traverse the `dp` array **from right to left**.
101
+
- Please think about whether it is possible to traverse the `dp` array from `left to right`?
102
+
4. According to the `dp` grid data, derive the "recursive formula".
5. Write a program and print the `dp` array. If it is not as expected, adjust it.
114
+
43
115
## Steps
44
116
45
117
1. Determine the **meaning** of the `dp[j]`
@@ -297,6 +369,43 @@ During the interview, you need to remember it. Is there any way to not worry abo
297
369
298
370
<details><summary>Click to view the answer</summary><p> As long as you copy the original `dp` and reference the value of the copy, you don't have to worry about the original `dp` value being modified. </p></details>
299
371
372
+
## Pattern of "Dynamic Programming"
373
+
374
+
`Dynamic programming` requires the use of the `dp` array to store the results. The value of `dp[i][j]` can be derived from the value of the previous `dp[x][y]` related to it.
375
+
376
+
#### "Dynamic programming" is divided into five steps
377
+
378
+
1. Determine the meaning of each value of the array `dp`.
379
+
2. Initialize the value of the array `dp`.
380
+
3. Fill in the `dp` grid data "in order" according to an example.
381
+
4. Based on the `dp` grid data, derive the "recursive formula".
382
+
5. Write a program and print the `dp` array. If it is not as expected, adjust it.
383
+
384
+
#### Detailed description of these five steps
385
+
386
+
1. Determine the meaning of each value of the array `dp`.
387
+
- First determine whether `dp` is a one-dimensional array or a two-dimensional array. A `one-dimensional rolling array` means that the values of the array are overwritten at each iteration. Most of the time, using `one-dimensional rolling array` instead of `two-dimensional array` can simplify the code; but for some problems, such as operating "two swappable arrays", for the sake of ease of understanding, it is better to use `two-dimensional array`.
388
+
- Try to use the meaning of the `return value` required by the problem as the meaning of `dp[i]` (one-dimensional) or `dp[i][j]` (two-dimensional). It works about 60% of the time. If it doesn't work, try other meanings.
389
+
- Try to save more information in the design. Repeated information only needs to be saved once in a `dp[i]`.
390
+
- Use simplified meanings. If the problem can be solved with `boolean value`, don't use `numeric value`.
391
+
2. Initialize the value of the array `dp`. The value of `dp` involves two levels:
392
+
1. The length of `dp`. Usually: `condition array length plus 1` or `condition array length`.
393
+
2. The value of `dp[i]` or `dp[i][j]`. `dp[0]` or `dp[0][0]` sometimes requires special treatment.
394
+
3. Fill in the `dp` grid data "in order" according to an example.
395
+
- The "recursive formula" is the core of the "dynamic programming" algorithm. But the "recursive formula" is obscure. If you want to get it, you need to make a table and use data to inspire yourself.
396
+
- If the original example is not good enough, you need to redesign one yourself.
397
+
- According to the example, fill in the `dp` grid data "in order", which is very important because it determines the traversal order of the code.
398
+
- Most of the time, from left to right, from top to bottom. But sometimes it is necessary to traverse from right to left, from bottom to top, from the middle to the right (or left), such as the "palindrome" problems. Sometimes, it is necessary to traverse a line twice, first forward and then backward.
399
+
- When the order is determined correctly, the starting point is determined. Starting from the starting point, fill in the `dp` grid data "in order". This order is also the order in which the program processes.
400
+
- In this process, you will get inspiration to write a "recursive formula". If you can already derive the formula, you do not need to complete the grid.
401
+
4. Based on the `dp` grid data, derive the "recursive formula".
402
+
- There are three special positions to pay attention to: `dp[i - 1][j - 1]`, `dp[i - 1][j]` and `dp[i][j - 1]`, the current `dp[i][j]` often depends on them.
403
+
- When operating "two swappable arrays", due to symmetry, we may need to use `dp[i - 1][j]` and `dp[i][j - 1]` at the same time.
404
+
5. Write a program and print the `dp` array. If it is not as expected, adjust it.
405
+
- Focus on analyzing those values that are not as expected.
406
+
407
+
After reading the above, do you feel that "dynamic programming" is not that difficult? Try to solve this problem. 🤗
0 commit comments