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: problems/0416-partition-equal-subset-sum.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@ Constraints:
29
29
* 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.
30
30
* This is actually a `01 Knapsack Problem` which belongs to `Dynamic Programming`. `Dynamic programming` means that the answer to the current problem can be derived from the previous similar problem. Therefore, the `dp` array is used to record all the answers.
31
31
32
-
* The core logic of the `01 Knapsack Problem` uses a two-dimensional `dp` array or a one-dimensional `dp`**rolling array**, first **iterate through the items**, then **iterate through the knapsack size**, then **reference the previous value corresponding to the size of current 'item'**.
32
+
* The core logic of the `01 Knapsack Problem` uses a two-dimensional `dp` array or a one-dimensional `dp`**rolling array**, first **iterate through the items**, then **iterate through the knapsack size** (`in reverse order` or use `dp.clone`), then **reference the previous value corresponding to the size of current 'item'**.
33
33
* There are many things to remember when using a two-dimensional `dp` array, and it is difficult to write it right at once during an interview, so I won't describe it here.
34
34
35
35
### Common steps in '01 Knapsack Problem'
@@ -94,7 +94,7 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
94
94
dp[j] = dp[j] || dp[j - nums[i]]
95
95
```
96
96
4. Determine the `dp` array's traversal order
97
-
* First **iterate through the items**, then **iterate through the knapsack size**.
97
+
* First **iterate through the items**, then **iterate through the knapsack size** (`in reverse order` or use `dp.clone`).
98
98
* When iterating through the knapsack size, since `dp[j]` depends on `dp[j]` and `dp[j - nums[i]]`, we should traverse the `dp` array **from right to left**.
99
99
* Please think if we can iterate through the `dp` array from `from left to right`? In the `Python` solution's code comments, I will answer this question.
100
100
5. Check the `dp` array's value
@@ -120,7 +120,7 @@ class Solution:
120
120
dp[0] =True
121
121
122
122
for num in nums:
123
-
# If traversing from left to right, the newly assigned value `dp[j]` will act as `dp[j - num]` later,
123
+
# If not traversing in reverse order, the newly assigned value `dp[j]` will act as `dp[j - num]` later,
124
124
# then the subsequent `dp[j]` will be affected. But each `num` can only be used once!
Copy file name to clipboardExpand all lines: problems/0494-target-sum.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,7 @@ it is recommended that you first work on another relatively simple question [416
43
43
## Thoughts
44
44
* When we see a set of numbers being used once to obtain another number through some calculation (just like this question), we can consider this to be a `01 Knapsack Problem`.
45
45
*`01 Knapsack Problem` belongs to `Dynamic Programming`. `Dynamic programming` means that the answer to the current problem can be derived from the previous similar problem. Therefore, the `dp` array is used to record all the answers.
46
-
* The core logic of the `01 Knapsack Problem` uses a two-dimensional `dp` array or a one-dimensional `dp`**rolling array**, first **traverses the items**, then **traverses the knapsack size**, then **reference the previous value corresponding to the size of current 'item'**.
46
+
* The core logic of the `01 Knapsack Problem` uses a two-dimensional `dp` array or a one-dimensional `dp`**rolling array**, first **traverses the items**, then **traverses the knapsack size** (`in reverse order` or use `dp.clone`), then **reference the previous value corresponding to the size of current 'item'**.
47
47
* There are many things to remember when using a two-dimensional `dp` array, and it is difficult to write it right at once during an interview, so I won't describe it here.
Copy file name to clipboardExpand all lines: problems/1049-last-stone-weight-ii.md
+6-6
Original file line number
Diff line number
Diff line change
@@ -47,7 +47,7 @@ Once you have completed `416`, it will be effortless to complete this question.
47
47
* So we need to change our thinking. Before you The question is equivalent to finding the minimum difference between the sums of the two arrays after splitting. If we find a subset array whose sum is closest to half of the sum of the complete array, then it is the subset array we want.
48
48
* Then this problem will become a `01 Knapsack Problem` which belongs to `Dynamic Programming`. `Dynamic programming` means that the answer to the current problem can be derived from the previous similar problem. Therefore, the `dp` array is used to record all the answers.
49
49
50
-
* The core logic of the `01 Knapsack Problem` uses a two-dimensional `dp` array or a one-dimensional `dp`**rolling array**, first **traverses the items**, then **traverses the knapsack**, then **reference the previous value corresponding to the size of current 'item'**.
50
+
* The core logic of the `01 Knapsack Problem` uses a two-dimensional `dp` array or a one-dimensional `dp`**rolling array**, first **traverses the items**, then **traverses the knapsack size** (`in reverse order` or use `dp.clone`), then **reference the previous value corresponding to the size of current 'item'**.
51
51
* There are many things to remember when using a two-dimensional `dp` array, and it is difficult to write it right at once during an interview, so I won't describe it here.
52
52
53
53
### Common steps in '01 Knapsack Problem'
@@ -107,8 +107,8 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
107
107
dp[j] = dp[j] || dp[j - stones[i]]
108
108
```
109
109
4. Determine the `dp` array's traversal order
110
-
*`dp[j]` depends on `dp[j]` and `dp[j - stones[i]]`, so we should traverse the `dp` array **from right to left**.
111
-
* Please think if we can traverse the `dp` array **from left to right**? In the `Python` solution's code comments, I will answer this question.
110
+
*`dp[j]` depends on `dp[j]` and `dp[j - stones[i]]`, so we should traverse the `dp` array **in reverse order**.
111
+
* Please think if we can traverse the `dp` array **not in reverse order**? In the `Python` solution's code comments, I will answer this question.
112
112
5. Check the `dp` array's value
113
113
* Print the `dp` to see if it is as expected.
114
114
@@ -127,7 +127,7 @@ class Solution:
127
127
dp[0] =True
128
128
129
129
for stone in stones:
130
-
# If traversing from left to right, the newly assigned value `dp[j]` will act as `dp[j - stone]` later,
130
+
# If not traversing in reverse order, the newly assigned value `dp[j]` will act as `dp[j - stone]` later,
131
131
# then the subsequent `dp[j]` will be affected. But each `stone` can only be used once!
132
132
for j inrange(len(dp) -1, 0, -1):
133
133
if j < stone:
@@ -139,7 +139,7 @@ class Solution:
139
139
return sum_ - i *2
140
140
```
141
141
142
-
As in the comment above, `for j in range(len(dp) - 1, 0, -1):`'s traversal order is **from right to left** which really matters.
142
+
As in the comment above, `for j in range(len(dp) - 1, 0, -1):`'s traversal order is **in reverse order** which really matters.
143
143
144
144
During the interview, you need to remember it. Is there any way to not worry about the traversal order?
145
145
@@ -167,7 +167,7 @@ class Solution:
167
167
return sum_ - i *2
168
168
```
169
169
170
-
* Personally, I like this approach because it makes the code logic clearer, does not need to consider the traversal direction,
170
+
* Personally, I like this approach because it makes the code logic clearer, does not need to consider the traversal order,
171
171
and is more applicable (you will encounter many situations in the future where backward iteration does not work).
0 commit comments