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
+38-51
Original file line number
Diff line number
Diff line change
@@ -230,15 +230,16 @@ end
230
230
231
231
## Intuition 2
232
232
233
-
- Solution 1 is essentially a "dynamic programming" algorithm implemented with rolling variables. It is easy to understand, and the space complexity is reduced to `O(1)`.
233
+
-`Solution 1` is essentially a "dynamic programming" algorithm implemented with `rolling variables`. It is easy to understand, and the space complexity is reduced to `O(1)`.
234
234
- But now, not only do we not reduce the dimension, but we also increase the dimension. It will be more difficult to understand and implement. So why do this thankless task?
235
235
<details><summary>Click to view the answer</summary><p> Because it can handle a more complex scenario (e.g. [583. Delete Operation for Two Strings](583-delete-operation-for-two-strings.md)) that is common in dynamic programming. </p></details>
236
-
- In this question, can we use a one-dimensional rolling array to implement the "dynamic programming" algorithm?
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>
236
+
- In this question, can we use a `one-dimensional rolling array` to implement the "dynamic programming" algorithm?
237
+
<details><summary>Click to view the answer</summary><p> Of course, but considering that for this problem 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
+
- The **compare two strings** question is about dealing with "two swappable arrays". After doing similar questions many times, we will form the intuition of using `two-dimensional arrays` for dynamic programming.
238
239
239
240
## Pattern of "Dynamic Programming"
240
241
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
+
"Dynamic Programming" requires the use of the `dp` array to store the results. The value of `dp[i][j]` can be converted from its previous (or multiple) values through a formula. Therefore, the value of `dp[i][j]`is derived step by step, and it is related to the previous `dp` record value.
242
243
243
244
#### "Dynamic programming" is divided into five steps
244
245
@@ -275,18 +276,10 @@ After reading the above, do you feel that "dynamic programming" is not that diff
275
276
276
277
## Steps
277
278
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.
279
-
280
-
### Common steps in dynamic programming
281
-
282
-
These five steps are a pattern for solving `dynamic programming` problems.
283
-
284
-
1. Determine the **meaning** of the `dp[i][j]`
285
-
- Since there are two strings, we can use two-dimensional arrays as the default option.
286
-
- At first, try to use the problem's `return` value as the value of `dp[i][j]` to determine the meaning of `dp[i][j]`. If it doesn't work, try another way.
279
+
1. Determine the **meaning** of the `dp[i][j]`.
287
280
-`dp[i][j]` represents whether the first `i` letters of `s` are a subsequence of `t`'s first `j` letters.
288
281
-`dp[i][j]` is `true` or `false`.
289
-
2. Determine the `dp` array's initial value
282
+
2. Determine the `dp` array's initial value.
290
283
- Use an example:
291
284
292
285
```
@@ -300,45 +293,39 @@ These five steps are a pattern for solving `dynamic programming` problems.
300
293
```
301
294
- `dp[0][j] = true` because `dp[0]` represents the empty string, and empty string is a subsequence of any string.
302
295
- `dp[i][j] = false (i != 0)`.
303
-
3. Determine the `dp` array's recurrence formula
304
-
- Try to complete the `dp` grid. In the process, you will get inspiration to derive the formula.
296
+
3. Fill in the `dp` grid data "in order" according to an example.
305
297
306
-
```
307
-
1. s = "a", t = "ahbgdc"
308
-
# a h b g d c
309
-
# T T T T T T T
310
-
# a F T T T T T T # dp[1]
311
-
```
312
-
```
313
-
2. s = "ab", t = "ahbgdc"
314
-
# a h b g d c
315
-
# T T T T T T T
316
-
# a F T T T T T T
317
-
# b F F F T T T T
318
-
```
319
-
```
320
-
3. s = "abc", t = "ahbgdc"
321
-
# a h b g d c
322
-
# T T T T T T T
323
-
# a F T T T T T T
324
-
# b F F F T T T T
325
-
# c F F F F F F T # dp[3]
326
-
```
327
-
- When analyzing the sample `dp` grid, remember there are three important points which you should pay special 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.
328
-
- If the question is also true in reverse (swap `s` and `t`), and we need to use `dp[i - 1][j]` or `dp[i][j - 1]`, then we probably need to use both of them.
329
-
- We can derive the `Recurrence Formula`:
298
+
```
299
+
1. s = "a", t = "ahbgdc"
300
+
# a h b g d c
301
+
# T T T T T T T
302
+
# a F T T T T T T # dp[1]
303
+
```
304
+
```
305
+
2. s = "ab", t = "ahbgdc"
306
+
# a h b g d c
307
+
# T T T T T T T
308
+
# a F T T T T T T
309
+
# b F F F T T T T
310
+
```
311
+
```
312
+
3. s = "abc", t = "ahbgdc"
313
+
# a h b g d c
314
+
# T T T T T T T
315
+
# a F T T T T T T
316
+
# b F F F T T T T
317
+
# c F F F F F F T # dp[3]
318
+
```
319
+
4. Based on the `dp` grid data, derive the "recursive formula".
330
320
331
-
```ruby
332
-
if s[i - 1] == t[j - 1]
333
-
dp[i][j] = dp[i - 1][j - 1]
334
-
else
335
-
dp[i][j] = dp[i][j - 1]
336
-
end
337
-
```
338
-
4. Determine the `dp` array's traversal order
339
-
- `dp[i][j]` depends on `dp[i - 1][j - 1]` and `dp[i][j - 1]`, so we should traverse the `dp` array from top to bottom, then from left to right.
340
-
5. Check the `dp` array's value
341
-
- Print the `dp` to see if it is as expected.
321
+
```ruby
322
+
if s[i - 1] == t[j - 1]
323
+
dp[i][j] = dp[i - 1][j - 1]
324
+
else
325
+
dp[i][j] = dp[i][j - 1]
326
+
end
327
+
```
328
+
5. Write a program and print the `dp` array. If it is not as expected, adjust it.
Copy file name to clipboardExpand all lines: en/1-1000/416-partition-equal-subset-sum.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@ Given an integer array `nums`, return `true` if you can partition the array into
42
42
43
43
## Pattern of "Dynamic Programming"
44
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.
45
+
"Dynamic Programming" requires the use of the `dp` array to store the results. The value of `dp[i][j]` can be converted from its previous (or multiple) values through a formula. Therefore, the value of `dp[i][j]`is derived step by step, and it is related to the previous `dp` record value.
46
46
47
47
#### "Dynamic programming" is divided into five steps
48
48
@@ -371,7 +371,7 @@ During the interview, you need to remember it. Is there any way to not worry abo
371
371
372
372
## Pattern of "Dynamic Programming"
373
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.
374
+
"Dynamic Programming" requires the use of the `dp` array to store the results. The value of `dp[i][j]` can be converted from its previous (or multiple) values through a formula. Therefore, the value of `dp[i][j]`is derived step by step, and it is related to the previous `dp` record value.
375
375
376
376
#### "Dynamic programming" is divided into five steps
Copy file name to clipboardExpand all lines: en/1-1000/474-ones-and-zeroes.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,7 @@ This question is difficult. It is recommended to complete a simple question of t
53
53
54
54
## Pattern of "Dynamic Programming"
55
55
56
-
`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.
56
+
"Dynamic Programming" requires the use of the `dp` array to store the results. The value of `dp[i][j]` can be converted from its previous (or multiple) values through a formula. Therefore, the value of `dp[i][j]`is derived step by step, and it is related to the previous `dp` record value.
57
57
58
58
#### "Dynamic programming" is divided into five steps
Copy file name to clipboardExpand all lines: en/1-1000/494-target-sum.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,7 @@ This problem is quite difficult if you have not solved similar problems before.
48
48
49
49
## Pattern of "Dynamic Programming"
50
50
51
-
`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.
51
+
"Dynamic Programming" requires the use of the `dp` array to store the results. The value of `dp[i][j]` can be converted from its previous (or multiple) values through a formula. Therefore, the value of `dp[i][j]`is derived step by step, and it is related to the previous `dp` record value.
52
52
53
53
#### "Dynamic programming" is divided into five steps
Copy file name to clipboardExpand all lines: en/1-1000/5-longest-palindromic-substring.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -60,7 +60,7 @@ If we use brute-force and check whether for every start and end position a subst
60
60
61
61
## Pattern of "Dynamic Programming"
62
62
63
-
`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.
63
+
"Dynamic Programming" requires the use of the `dp` array to store the results. The value of `dp[i][j]` can be converted from its previous (or multiple) values through a formula. Therefore, the value of `dp[i][j]`is derived step by step, and it is related to the previous `dp` record value.
64
64
65
65
#### "Dynamic programming" is divided into five steps
Copy file name to clipboardExpand all lines: en/1-1000/53-maximum-subarray.md
+28-42
Original file line number
Diff line number
Diff line change
@@ -45,18 +45,15 @@ Given an integer array `nums`, find the `subarray` with the largest sum, and ret
45
45
46
46
## Intuition 1
47
47
48
-
- This problem can be solved by using `Greedy Algorithm` (please see `solution 2`), but here we will use another way.
49
-
- Imagine the size of nums is `i`, let us consider if the same question is applied to the `subarray` of `nums` from index `0` to `i - 1`.
50
-
- The answer is `yes`. Then let us think if the `i - 1`'s answer could impact the answer of `i`.
51
-
- The answer is still `yes`. What would be the impact?
52
-
- For `nums[i]`,
48
+
- This problem can be solved by using `Greedy Algorithm` (please see `solution 2`), yet here we will use another way.
49
+
- For `nums[i]`:
53
50
1. If the `previous sum` is negative, we can discard `previous sum`;
54
51
2. If the `previous sum` is positive, we can add `previous sum` to the `current sum`.
55
-
-So we can use `Dynamic Programming` to solve the problem. The characteristic of the "Dynamic Programming" algorithm is that the value of `dp[i]` is converted from `dp[i - 1]`.
52
+
-Therefore, it meets the characteristics of a `dynamic programming` problem.
56
53
57
54
## Pattern of "Dynamic Programming"
58
55
59
-
`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.
56
+
"Dynamic Programming" requires the use of the `dp` array to store the results. The value of `dp[i][j]` can be converted from its previous (or multiple) values through a formula. Therefore, the value of `dp[i][j]`is derived step by step, and it is related to the previous `dp` record value.
60
57
61
58
#### "Dynamic programming" is divided into five steps
62
59
@@ -93,47 +90,36 @@ After reading the above, do you feel that "dynamic programming" is not that diff
93
90
94
91
## Steps
95
92
96
-
### Common steps in dynamic programming
97
-
98
-
These five steps are a pattern for solving `dynamic programming` problems.
99
-
100
93
1. Determine the **meaning** of the `dp[i]`
101
-
- At first, try to use the problem's `return` value as the value of `dp[i]` to determine the meaning of `dp[i]`. If it doesn't work, try another way.
102
94
- Imagine that `dp[i]` represents the `largest sum` at index `i`. Is this okay?
103
95
mark-detail `dp[i + 1]` cannot be calculated by `dp[i]`. So we have to change the meaning. mark-detail
104
96
- How to design it?
105
97
mark-detail If `dp[i]` represents the `current sum` at index `i`, `dp[i + 1]` can be calculated by `dp[i]`. Finally, we can see that the `maximum sum` is recorded in the `current sum` array. mark-detail
106
98
2. Determine the `dp` array's initial value
107
-
- Use an example:
108
-
109
-
```ruby
110
-
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
111
-
dp = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
112
-
```
113
-
-`dp[i] = nums[i]` would be good.
114
-
3. Determine the `dp` array's recurrence formula
115
-
- Try to complete the `dp` array. In the process, you will get inspiration to derive the formula.
116
-
117
-
```ruby
118
-
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
119
-
dp = [-2, 1, N, N, N, N, N, N, N] # N means don't pay attention to it now
120
-
dp = [-2, 1, -2, N, N, N, N, N, N]
121
-
dp = [-2, 1, -2, 4, N, N, N, N, N]
122
-
dp = [-2, 1, -2, 4, 3, N, N, N, N]
123
-
dp = [-2, 1, -2, 4, 3, 5, N, N, N]
124
-
dp = [-2, 1, -2, 4, 3, 5, 6, N, N]
125
-
dp = [-2, 1, -2, 4, 3, 5, 6, 1, N]
126
-
dp = [-2, 1, -2, 4, 3, 5, 6, 1, 5]
127
-
```
128
-
-After analyzing the sample `dp` array, we can derive the `Recurrence Formula`:
129
-
130
-
```python
131
-
dp[i] = max(nums[i], dp[i - 1] + nums[i])
132
-
```
133
-
4. Determine the `dp` array's traversal order
134
-
- `dp[i]` depends on `dp[i - 1]`, so we should traverse the `dp` array from left to right.
135
-
5. Check the `dp` array's value
136
-
-Print the `dp` to see if it is as expected.
99
+
100
+
```ruby
101
+
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
102
+
dp = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
103
+
```
104
+
3. Fillin the `dp` grid data "in order" according to an example.
105
+
106
+
```ruby
107
+
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
108
+
dp = [-2, 1, N, N, N, N, N, N, N] # N means don't pay attention to it now
109
+
dp = [-2, 1, -2, N, N, N, N, N, N]
110
+
dp = [-2, 1, -2, 4, N, N, N, N, N]
111
+
dp = [-2, 1, -2, 4, 3, N, N, N, N]
112
+
dp = [-2, 1, -2, 4, 3, 5, N, N, N]
113
+
dp = [-2, 1, -2, 4, 3, 5, 6, N, N]
114
+
dp = [-2, 1, -2, 4, 3, 5, 6, 1, N]
115
+
dp = [-2, 1, -2, 4, 3, 5, 6, 1, 5]
116
+
```
117
+
4. Based on the `dp` grid data, derive the "recursive formula".
118
+
119
+
```python
120
+
dp[i] = max(nums[i], dp[i - 1] + nums[i])
121
+
```
122
+
5. Write a program andprint the `dp` array. If it is not as expected, adjust it.
Copy file name to clipboardExpand all lines: en/1-1000/583-delete-operation-for-two-strings.md
+35-46
Original file line number
Diff line number
Diff line change
@@ -34,11 +34,12 @@ In one **step**, you can delete exactly one character in either string.
34
34
35
35
## Intuition
36
36
37
-
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`.
37
+
It is a question of **comparing two strings** which is about dealing with "two swappable arrays".
38
+
After doing similar questions many times, we will form the intuition of using `two-dimensional arrays` for dynamic programming.
38
39
39
40
## Pattern of "Dynamic Programming"
40
41
41
-
`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.
42
+
"Dynamic Programming" requires the use of the `dp` array to store the results. The value of `dp[i][j]` can be converted from its previous (or multiple) values through a formula. Therefore, the value of `dp[i][j]`is derived step by step, and it is related to the previous `dp` record value.
42
43
43
44
#### "Dynamic programming" is divided into five steps
44
45
@@ -75,16 +76,10 @@ After reading the above, do you feel that "dynamic programming" is not that diff
75
76
76
77
## Steps
77
78
78
-
### Common steps in dynamic programming
79
-
80
-
These five steps are a pattern for solving `dynamic programming` problems.
81
-
82
-
1. Determine the **meaning** of the `dp[i][j]`
83
-
- Since there are two strings, we can use two-dimensional arrays as the default option.
84
-
- At first, try to use the problem's `return` value as the value of `dp[i][j]` to determine the meaning of `dp[i][j]`. If it doesn't work, try another way.
79
+
1. Determine the **meaning** of the `dp[i][j]`.
85
80
-`dp[i][j]` represents the **minimum** number of steps required to make `word1`'s first `i` letters and `word2`'s first `j` letters the same.
86
81
-`dp[i][j]` is an integer.
87
-
2. Determine the `dp` array's initial value
82
+
2. Determine the `dp` array's initial value.
88
83
- Use an example:
89
84
90
85
```
@@ -97,44 +92,38 @@ These five steps are a pattern for solving `dynamic programming` problems.
97
92
```
98
93
- `dp[0][j] = j`, because `dp[0]` represents the empty string, and the number of steps is just the number of chars to be deleted.
99
94
- `dp[i][0] = i`, the reason is the same as the previous line, just viewed in vertical direction.
100
-
3. Determine the `dp` array's recurrence formula
101
-
- Try to complete the grid. In the process, you will get inspiration to derive the formula.
95
+
3. Fill in the `dp` grid data "in order" according to an example.
102
96
103
-
```
104
-
1. word1 = "s", word2 = "eat"
105
-
# e a t
106
-
# 0 1 2 3
107
-
# s 1 2 3 4 # dp[1]
108
-
```
109
-
```
110
-
2. word1 = "se", word2 = "eat"
111
-
# e a t
112
-
# 0 1 2 3
113
-
# s 1 2 3 4
114
-
# e 2 1 2 3
115
-
```
116
-
```
117
-
3. word1 = "sea", word2 = "eat"
118
-
# e a t
119
-
# 0 1 2 3
120
-
# s 1 2 3 4
121
-
# e 2 1 2 3
122
-
# a 3 2 1 2
123
-
```
124
-
- When analyzing the sample `dp` grid, remember there are three important points which you should pay special 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.
125
-
- If the question is also true in reverse (swap `word1` and `word2`), and we need to use `dp[i - 1][j]` or `dp[i][j - 1]`, then we probably need to use both of them.
126
-
- We can derive the `Recurrence Formula`:
97
+
```
98
+
1. word1 = "s", word2 = "eat"
99
+
# e a t
100
+
# 0 1 2 3
101
+
# s 1 2 3 4 # dp[1]
102
+
```
103
+
```
104
+
2. word1 = "se", word2 = "eat"
105
+
# e a t
106
+
# 0 1 2 3
107
+
# s 1 2 3 4
108
+
# e 2 1 2 3
109
+
```
110
+
```
111
+
3. word1 = "sea", word2 = "eat"
112
+
# e a t
113
+
# 0 1 2 3
114
+
# s 1 2 3 4
115
+
# e 2 1 2 3
116
+
# a 3 2 1 2
117
+
```
118
+
4. Based on the `dp` grid data, derive the "recursive formula".
127
119
128
-
```python
129
-
if word1[i - 1] == word2[j - 1]
130
-
dp[i][j] = dp[i - 1][j - 1]
131
-
else
132
-
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1
133
-
```
134
-
4. Determine the `dp` array's traversal order
135
-
- `dp[i][j]` depends on `dp[i - 1][j - 1]`, `dp[i - 1][j]` and `dp[i][j - 1]`, so we should traverse the `dp` array from top to bottom, then from left to right.
136
-
5. Check the `dp` array's value
137
-
- Print the `dp` to see if it is as expected.
120
+
```python
121
+
if word1[i - 1] == word2[j - 1]
122
+
dp[i][j] = dp[i - 1][j - 1]
123
+
else
124
+
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1
125
+
```
126
+
5. Write a program and print the `dp` array. If it is not as expected, adjust it.
Copy file name to clipboardExpand all lines: en/1001-2000/1049-last-stone-weight-ii.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,7 @@ we can combine 1 and 1 to get 0, so the array converts to [1], then that's t
67
67
68
68
## Pattern of "Dynamic Programming"
69
69
70
-
`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.
70
+
"Dynamic Programming" requires the use of the `dp` array to store the results. The value of `dp[i][j]` can be converted from its previous (or multiple) values through a formula. Therefore, the value of `dp[i][j]`is derived step by step, and it is related to the previous `dp` record value.
71
71
72
72
#### "Dynamic programming" is divided into five steps
0 commit comments