Skip to content

Commit 09d2a24

Browse files
committed
0416 0494 1049 Reworded about the in reverse order.
1 parent e53f083 commit 09d2a24

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

problems/0416-partition-equal-subset-sum.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Constraints:
2929
* 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.
3030
* 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.
3131

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'**.
3333
* 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.
3434

3535
### Common steps in '01 Knapsack Problem'
@@ -94,7 +94,7 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
9494
dp[j] = dp[j] || dp[j - nums[i]]
9595
```
9696
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`).
9898
* 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**.
9999
* 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.
100100
5. Check the `dp` array's value
@@ -120,7 +120,7 @@ class Solution:
120120
dp[0] = True
121121

122122
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,
124124
# then the subsequent `dp[j]` will be affected. But each `num` can only be used once!
125125
for j in range(len(dp) - 1, 0, -1):
126126
if j < num:

problems/0494-target-sum.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ it is recommended that you first work on another relatively simple question [416
4343
## Thoughts
4444
* 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`.
4545
* `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'**.
4747
* 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.
4848

4949
### Common steps in '01 Knapsack Problem'

problems/1049-last-stone-weight-ii.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Once you have completed `416`, it will be effortless to complete this question.
4747
* 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.
4848
* 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.
4949

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'**.
5151
* 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.
5252

5353
### Common steps in '01 Knapsack Problem'
@@ -107,8 +107,8 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
107107
dp[j] = dp[j] || dp[j - stones[i]]
108108
```
109109
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.
112112
5. Check the `dp` array's value
113113
* Print the `dp` to see if it is as expected.
114114

@@ -127,7 +127,7 @@ class Solution:
127127
dp[0] = True
128128

129129
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,
131131
# then the subsequent `dp[j]` will be affected. But each `stone` can only be used once!
132132
for j in range(len(dp) - 1, 0, -1):
133133
if j < stone:
@@ -139,7 +139,7 @@ class Solution:
139139
return sum_ - i * 2
140140
```
141141

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.
143143

144144
During the interview, you need to remember it. Is there any way to not worry about the traversal order?
145145

@@ -167,7 +167,7 @@ class Solution:
167167
return sum_ - i * 2
168168
```
169169

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,
171171
and is more applicable (you will encounter many situations in the future where backward iteration does not work).
172172

173173
## C++

0 commit comments

Comments
 (0)