Skip to content

Commit 9ef7bb9

Browse files
committed
Moved the derive formula after initialize dp.
1 parent 9db4eaa commit 9ef7bb9

4 files changed

+50
-34
lines changed

0053-maximum-subarray.md

+25-19
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ These five steps are a pattern for solving dynamic programming problems.
2828
* 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.
2929
* Imagine that `dp[i]` represents the `largest sum` at index `i`. The `dp[i + 1]` cannot be calculated by `dp[i]`. So we have to change this meaning.
3030
* Then consider that `dp[i]` represents the `current sum` at index `i`. We can see the `largest sum` is recorded in the `current sum` array. It may work.
31-
2. Determine the `dp` array's recurrence formula
32-
* `dp[i] = max(nums[i], dp[i - 1] + nums[i])`.
33-
3. Determine the `dp` array's initial value
31+
2. Determine the `dp` array's initial value
3432
* `dp[i] = nums[i]` would be good.
33+
3. Determine the `dp` array's recurrence formula
34+
* Use an example:
35+
```
36+
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
37+
dp = [-2, 1, -2, 4, 3, 5, 6, 1, 5]
38+
```
39+
* After analyzing the sample `dp` data, we can derive the `recurrence formula`:
40+
* `dp[i] = max(nums[i], dp[i - 1] + nums[i])`.
3541
4. Determine the `dp` array's traversal order
3642
* `dp[i]` depends on `dp[i - 1]`, so we should traverse the `dp` array from left to right.
3743
5. Check the `dp` array's value
@@ -41,6 +47,22 @@ These five steps are a pattern for solving dynamic programming problems.
4147
* Time: `O(n)`.
4248
* Space: `O(n)`.
4349

50+
## Java
51+
52+
```java
53+
class Solution {
54+
int[] dp = nums.clone();
55+
int result = dp[0];
56+
57+
for (int i = 1; i < dp.length; i++) {
58+
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
59+
if (dp[i] > result) result = dp[i];
60+
}
61+
62+
return result; // or 'return Arrays.stream(dp).max().getAsInt();'
63+
}
64+
```
65+
4466
## Python
4567
```python
4668
class Solution:
@@ -69,22 +91,6 @@ public:
6991
};
7092
```
7193
72-
## Java
73-
74-
```java
75-
class Solution {
76-
int[] dp = nums.clone();
77-
int result = dp[0];
78-
79-
for (int i = 1; i < dp.length; i++) {
80-
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
81-
if (dp[i] > result) result = dp[i];
82-
}
83-
84-
return result; // or 'return Arrays.stream(dp).max().getAsInt();'
85-
}
86-
```
87-
8894
## C#
8995
```c#
9096
public class Solution {

0392-is-subsequence.md

+15-6
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,36 @@ These five steps are a pattern for solving dynamic programming problems.
2828
* 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.
2929
* `dp[i][j]` represents whether the first `i` letters of `s` are a subsequence of `t`'s first `j` letters.
3030
* The value of `dp[i][j]` is `true` or `false`.
31-
2. Determine the `dp` array's recurrence formula
31+
2. Determine the `dp` array's initial value
3232
* Use an example:
3333
```
34+
After initialized, the 'dp' array would be:
35+
s = "abc", t = "ahbgdc"
36+
# a h b g d c
37+
# T T T T T T T # dp[0]
38+
# a F F F F F F F
39+
# b F F F F F F F
40+
# c F F F F F F F
41+
```
42+
* `dp[0][j] = true` because `dp[0]` represents the empty string, and empty string is a subsequence of any string.
43+
* `dp[i][j] = false (i != 0)`.
44+
3. Determine the `dp` array's recurrence formula
45+
```
46+
The final 'dp' array would be:
3447
s = "abc", t = "ahbgdc"
3548
# a h b g d c
3649
# T T T T T T T
3750
# a F T T T T T T
3851
# b F F F T T T T
3952
# c F F F F F F T
4053
```
41-
* Recurrence formula:
54+
* After analyzing the sample `dp` data, we can derive the `recurrence formula`:
4255
```
4356
if s[i - 1] == t[j - 1]
4457
dp[i][j] = dp[i - 1][j - 1]
4558
else
4659
dp[i][j] = dp[i][j - 1]
4760
```
48-
49-
3. Determine the `dp` array's initial value
50-
* `dp[0][j] = true` because `dp[0]` represents the empty string and empty string is a subsequence of any string.
51-
* `dp[i][j] = false (i != 0)`.
5261
4. Determine the `dp` array's traversal order
5362
* `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.
5463
5. Check the `dp` array's value

0583-delete-operation-for-two-strings.md

+7-9
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@ These five steps are a pattern for solving dynamic programming problems.
2828
* 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.
2929
* `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.
3030
* The value of `dp[i][j]` is an integer.
31-
2. Determine the `dp` array's recurrence formula
32-
* Use an example:
31+
2. Determine the `dp` array's initial value
32+
* Use an example:
3333
```
3434
After initialized, the 'dp' array would be:
3535
# e a t
36-
# 0 1 2 3 # dp[0] is for empty string, the number of steps is just the number of chars to be deleted
36+
# 0 1 2 3 # dp[0]
3737
# s 1 0 0 0
3838
# e 2 0 0 0
3939
# a 3 0 0 0
4040
```
41-
41+
* `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
42+
* `dp[i][0] = i`, the reason is the same as previous line, yet in vertical direction.
43+
3. Determine the `dp` array's recurrence formula
4244
```
4345
The final 'dp' array would be:
4446
# e a t
@@ -47,17 +49,13 @@ The final 'dp' array would be:
4749
# e 2 1 2 3
4850
# a 3 2 1 2
4951
```
50-
* Recurrence formula:
52+
* After analyzing the sample `dp` data, we can derive the `recurrence formula`:
5153
```python
5254
if word1[i - 1] == word2[j - 1]
5355
dp[i][j] = dp[i - 1][j - 1]
5456
else
5557
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1
5658
```
57-
58-
3. Determine the `dp` array's initial value
59-
* `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
60-
* `dp[i][0] = i`, the reason is the same as previous line, yet in vertical direction.
6159
4. Determine the `dp` array's traversal order
6260
* `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.
6361
5. Check the `dp` array's value

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# A best practice for the LeetCode problems
22
- [53. Maximum Subarray](./0053-maximum-subarray.md)
33
- [392. Is Subsequence](./0392-is-subsequence.md)
4+
- [583. Delete Operation for Two Strings](./0583-delete-operation-for-two-strings.md)
5+
6+
- More LeetCode problems will be added soon...

0 commit comments

Comments
 (0)