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/416-partition-equal-subset-sum.md
+45-59Lines changed: 45 additions & 59 deletions
Original file line number
Diff line number
Diff line change
@@ -38,27 +38,20 @@ Given an integer array `nums`, return `true` if you can partition the array into
38
38
##Intuition 1
39
39
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
-
- This is actually a`0/1 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.
42
-
- The core logic of the`0/1 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'**.
43
-
- 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.
41
+
- This problem can be solved using the`0/1 knapsack problem` algorithm.
44
42
45
43
##Steps
46
44
47
-
###Common steps in '0/1 Knapsack Problem'
48
-
These five steps are a pattern for solving`Dynamic Programming` problems.
49
-
50
45
1. Determine the**meaning** of the`dp[j]`
51
-
- We can use a one-dimensional`dp`**rolling array**. Rolling an array means that the values of the array are overwritten each time through the iteration.
52
-
- At first, try to use the problem's`return` value as the value of`dp[j]` to determine the meaning of`dp[j]`. If it doesn't work, try another way.
53
-
- So,`dp[j]` represents whether it is possible to`sum` the first`i``nums` to get`j`.
46
+
-`dp[j]` represents whether it is possible to`sum` the first`i``nums` to get`j`.
54
47
-`dp[j]` is a boolean.
55
48
56
49
2. Determine the`dp` array's initial value
57
50
- Use an example:
58
51
59
52
```
60
53
nums = [1,5,11,5], so 'half of the sum' is 11.
61
-
The `size` of the knapsack is `half of the sum`, and the `items` are `nums`.
54
+
The `size` of the knapsack is `11 + 1`, and the `items` are `nums`.
62
55
So after initialization, the 'dp' array would be:
63
56
# 0 1 2 3 4 5 6 7 8 9 10 11
64
57
# T F F F F F F F F F F F # dp
@@ -67,57 +60,50 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
67
60
# 11
68
61
# 5
69
62
```
70
-
- You can see the `dp` array size is one greater than the knapsack size. In this way, the knapsack size and index value are equal, which helps to understand.
71
63
- `dp[0]` is set to `true`, indicating that an empty knapsack can be achieved by not putting any items in it. In addition, it is used as the starting value, and the subsequent `dp[j]` will depend on it. If it is `false`, all values of `dp[j]` will be `false`.
72
64
- `dp[j] = false (j != 0)`, indicating that it is impossible to get `j` with no `nums`.
73
-
3. Determine the `dp` array's recurrence formula
74
-
- Try to complete the grid. In the process, you will get inspiration to derive the formula.
75
-
76
-
```
77
-
1. Use the first num '1'.
78
-
# 0 1 2 3 4 5 6 7 8 9 10 11
79
-
# T F F F F F F F F F F F
80
-
# 1 T T F F F F F F F F F F # dp
81
-
```
82
-
83
-
```
84
-
2. Use the second num '5'.
85
-
# 0 1 2 3 4 5 6 7 8 9 10 11
86
-
# T F F F F F F F F F F F
87
-
# 1 T T F F F F F F F F F F
88
-
# 5 T T F F F T T F F F F F
89
-
```
90
-
91
-
```
92
-
3. Use the third num '11'.
93
-
# 0 1 2 3 4 5 6 7 8 9 10 11
94
-
# T F F F F F F F F F F F
95
-
# 1 T T F F F F F F F F F F
96
-
# 5 T T F F F T T F F F F F
97
-
# 11 T T F F F T T F F F F T
98
-
```
99
-
100
-
```
101
-
3. Use the last num '5'.
102
-
# 0 1 2 3 4 5 6 7 8 9 10 11
103
-
# T F F F F F F F F F F F
104
-
# 1 T T F F F F F F F F F F
105
-
# 5 T T F F F T T F F F F F
106
-
# 11 T T F F F T T F F F F T
107
-
# 5 T T F F F T T F F F T T # dp
108
-
```
109
-
- After analyzing the sample `dp` grid, we can derive the `Recurrence Formula`:
110
-
111
-
```cpp
112
-
dp[j] = dp[j] || dp[j - nums[i]]
113
-
```
114
-
115
-
4. Determine the `dp` array's traversal order
116
-
- First **iterate through the items**, then **iterate through the knapsack size** (`in reverse order` or use `dp.clone`).
117
-
- 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**.
118
-
- 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.
119
-
5. Check the `dp` array's value
120
-
- Print the `dp` to see if it is as expected.
65
+
3. Fill in the `dp` grid data "in order" according to an example.
66
+
67
+
```
68
+
1. Use the first num '1'.
69
+
# 0 1 2 3 4 5 6 7 8 9 10 11
70
+
# T F F F F F F F F F F F
71
+
# 1 T T F F F F F F F F F F # dp
72
+
```
73
+
74
+
```
75
+
2. Use the second num '5'.
76
+
# 0 1 2 3 4 5 6 7 8 9 10 11
77
+
# T F F F F F F F F F F F
78
+
# 1 T T F F F F F F F F F F
79
+
# 5 T T F F F T T F F F F F
80
+
```
81
+
82
+
```
83
+
3. Use the third num '11'.
84
+
# 0 1 2 3 4 5 6 7 8 9 10 11
85
+
# T F F F F F F F F F F F
86
+
# 1 T T F F F F F F F F F F
87
+
# 5 T T F F F T T F F F F F
88
+
# 11 T T F F F T T F F F F T
89
+
```
90
+
91
+
```
92
+
3. Use the last num '5'.
93
+
# 0 1 2 3 4 5 6 7 8 9 10 11
94
+
# T F F F F F F F F F F F
95
+
# 1 T T F F F F F F F F F F
96
+
# 5 T T F F F T T F F F F F
97
+
# 11 T T F F F T T F F F F T
98
+
# 5 T T F F F T T F F F T T # dp
99
+
```
100
+
4. Based on the `dp` grid data, derive the "recursive formula".
101
+
102
+
```cpp
103
+
dp[j] = dp[j] || dp[j - nums[i]]
104
+
```
105
+
106
+
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/474-ones-and-zeroes.md
+9-24Lines changed: 9 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -53,11 +53,9 @@ This question is difficult. It is recommended to complete a simple question of t
53
53
54
54
##Steps
55
55
56
-
###Common steps in '0/1 Knapsack Problem'
57
-
These five steps are a pattern for solving`Dynamic Programming` problems.
58
-
59
56
1. Determine the**meaning** of the`dp[j]`
60
57
- Since we are only considering the zero count constraint for now, we can use a one-dimensional`dp` array.
58
+
-`items` is`strs`,`backpack` is`max_zero_count`.
61
59
-`dp[j]` represents the maximum number of strings that can be selected with at most`j` zeros.
62
60
-`dp[j]` is an integer.
63
61
@@ -68,11 +66,10 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
68
66
max_zero_count= m
69
67
dp= [0]* (max_zero_count+1)
70
68
```
71
-
- The`dp` array sizeis one greater than the zero count constraint. This way, the index value equals the constraint value, making it easier to understand.
72
69
-`dp[0] =0`, indicating thatwith no zeros, we can select0 strings.
73
70
-`dp[j] =0`as the initial value because we will use`max` to increase them later.
74
71
75
-
3.Determinethe`dp`array's recurrence formula
72
+
3.According to an example, fillinthe`dp`grid data"in order".
76
73
- Let's analyze the example step by step:
77
74
78
75
```
@@ -100,25 +97,13 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
100
97
# 0 1 2 3 4 5
101
98
# 0 2 3 3 4 4
102
99
```
103
-
- After analyzing the sample`dp` grid, we can derive the`Recurrence Formula`:
104
-
105
-
```cpp
106
-
dp[j]=max(dp[j], dp[j- zero_count]+1)
107
-
```
108
-
- This formula means:for each string, we can either:
109
-
1. Not select it (keep the current value`dp[j]`)
110
-
2. Select it (add1 to the value at`dp[j- zero_count]`)
100
+
4. According to the`dp` grid data, derive the"recursive formula".
111
101
112
-
4. Determine the`dp` array's traversal order
113
-
- First**iterate through the strings**, then**iterate through the zero count** (`in reverse order`).
114
-
- When iterating through the zero count, since`dp[j]` depends on`dp[j]`and`dp[j- zero_count]`, we should traverse**from right to left**.
115
-
- This ensures that we don't use the same string multiple times.
116
-
117
-
5. Check the`dp` array's value
118
-
- Print the`dp` to seeif itisas expected.
119
-
- The final answer will be at`dp[max_zero_count]`.
120
-
121
-
6. The code that only considers the quantity limit of`0`is:
102
+
```cpp
103
+
dp[j]=max(dp[j], dp[j- zero_count]+1)
104
+
```
105
+
5. Write a programandprint the`dp` array. If itisnotas expected, adjust it.
106
+
- The code that only considers the quantity limit of`0`is:
122
107
123
108
```python
124
109
classSolution:
@@ -144,7 +129,7 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
144
129
return zero_count
145
130
```
146
131
147
-
### Now, you can consider another dimension: the quantity limit of `1`.
132
+
#### Now, you can consider another dimension: the quantity limit of `1`.
148
133
149
134
It should be handled similarly to`0` butin another dimension. Please see the complete code below.
Copy file name to clipboardExpand all lines: en/1-1000/494-target-sum.md
+49-64Lines changed: 49 additions & 64 deletions
Original file line number
Diff line number
Diff line change
@@ -46,24 +46,14 @@ Return the number of different **expressions** that you can build, which evaluat
46
46
47
47
This problem is quite difficult if you have not solved similar problems before. So before you start working on this question, it is recommended that you first work on another relatively simple question[416. Partition Equal Subset Sum](416-partition-equal-subset-sum.md) that is similar to this one.
48
48
49
-
- 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`0/1 Knapsack Problem`.
50
-
-`0/1 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.
51
-
- The core logic of the`0/1 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](416-partition-equal-subset-sum.md) or use`dp.clone`), then**reference the previous value corresponding to the size of current 'item'**.
52
-
- 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.
53
-
54
49
##Steps
55
50
56
-
###Common steps in '0/1 Knapsack Problem'
57
-
These five steps are a pattern for solving`Dynamic Programming` problems.
58
-
59
51
1. Determine the**meaning** of the`dp[j]`
60
-
- We can use a one-dimensional`dp`**rolling array**. Rolling an array means that the values of the array are overwritten each time through the iteration.
61
-
- At first, try to use the problem's`return` value as the value of`dp[j]` to determine the meaning of`dp[j]`. If it doesn't work, try another way.
62
-
- So,`dp[j]` represents that by using the**first**`i` nums, the**number** of different**expressions** that you can build, which evaluates to`j`.
52
+
-`dp[j]` represents that by using the**first**`i` nums, the**number** of different**expressions** that you can build, which evaluates to`j`.
63
53
-`dp[j]` is an**integer**.
64
54
2. Determine the`dp` array's initial value
65
55
- Use an example. We didn't use the`Example 1: Input: nums = [1,1,1,1,1], target = 3` because it is too special and is not a good example for deriving a formula.
66
-
- I made up an example:`nums = [1,2,1,2], target = 4`. The example must be simple, otherwise it would take too long to complete the grid.
56
+
- I made up an example:`nums = [1,2,1,2], target = 4`.
67
57
- First, determine the`size` of the knapsack.
68
58
- The`target` value may be very small, such as`0`, so it alone cannot determine the`size` of the knapsack.
69
59
- The sum of`nums` should also be taken into account to fully cover all knapsack sizes.
@@ -73,66 +63,61 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
73
63
74
64
```
75
65
So after initialization, the 'dp' array would be:
76
-
# 0 1 2 3 4 5 6
77
-
# 1 0 0 0 0 0 0 # dp
66
+
#0 1 2 3 4 5 6
67
+
#1 0 0 0 0 0 0 # dp
78
68
# 1
79
69
# 2
80
70
# 1
81
71
# 2
82
72
```
83
-
- You can see the `dp` array size is **one** greater than the knapsack size. In this way, the knapsack size and index value are equal, which helps to understand.
84
73
- `dp[0]` is set to `1`, indicating that an empty knapsack can be achieved by not using any `nums`. In addition, it is used as the starting value, and the subsequent `dp[j]` will depend on it. If it is `0`, all values of `dp[j]` will be `0`.
85
74
- `dp[j] = 0 (j != 0)`, indicating that it is impossible to get `j` with no `nums`.
86
-
3. Determine the `dp` array's recurrence formula
87
-
- Try to complete the grid. In the process, you will get inspiration to derive the formula.
88
-
89
-
```
90
-
1. Use the first num '1'.
91
-
# 0 1 2 3 4 5 6
92
-
# 1 0 0 0 0 0 0
93
-
# 1 0 1 0 0 0 0 0 # dp
94
-
# 2
95
-
# 1
96
-
# 2
97
-
```
98
-
```
99
-
2. Use the second num '2'.
100
-
# 0 1 2 3 4 5 6
101
-
# 1 0 0 0 0 0 0
102
-
# 1 0 1 0 0 0 0 0
103
-
# 2 0 1 0 1 0 0 0
104
-
# 1
105
-
# 2
106
-
```
107
-
```
108
-
3. Use the third num '1'.
109
-
# 0 1 2 3 4 5 6
110
-
# 1 0 0 0 0 0 0
111
-
# 1 0 1 0 0 0 0 0
112
-
# 2 0 1 0 1 0 0 0
113
-
# 1 2 0 2 0 1 0 0
114
-
# 2
115
-
```
116
-
```
117
-
4. Use the fourth num '2'.
118
-
# 0 1 2 3 4 5 6
119
-
# 1 0 0 0 0 0 0
120
-
# 1 0 1 0 0 0 0 0
121
-
# 2 0 1 0 1 0 0 0
122
-
# 1 2 0 2 0 1 0 0
123
-
# 2 4 0 3 0 2 0 1 # dp
124
-
```
125
-
- After analyzing the sample `dp` grid, we can derive the `Recurrence Formula`:
126
-
127
-
```java
128
-
dp[j] = dp[abs(j - nums[i])] + dp[j + nums[i]]
129
-
```
75
+
3. According to an example, fill in the `dp` grid data "in order".
76
+
77
+
```
78
+
1. Use the first num '1'.
79
+
# 0 1 2 3 4 5 6
80
+
# 1 0 0 0 0 0 0
81
+
# 1 0 1 0 0 0 0 0 # dp
82
+
# 2
83
+
# 1
84
+
# 2
85
+
```
86
+
```
87
+
2. Use the second num '2'.
88
+
# 0 1 2 3 4 5 6
89
+
# 1 0 0 0 0 0 0
90
+
# 1 0 1 0 0 0 0 0
91
+
# 2 0 1 0 1 0 0 0
92
+
# 1
93
+
# 2
94
+
```
95
+
```
96
+
3. Use the third num '1'.
97
+
# 0 1 2 3 4 5 6
98
+
# 1 0 0 0 0 0 0
99
+
# 1 0 1 0 0 0 0 0
100
+
# 2 0 1 0 1 0 0 0
101
+
# 1 2 0 2 0 1 0 0
102
+
# 2
103
+
```
104
+
```
105
+
4. Use the fourth num '2'.
106
+
# 0 1 2 3 4 5 6
107
+
# 1 0 0 0 0 0 0
108
+
# 1 0 1 0 0 0 0 0
109
+
# 2 0 1 0 1 0 0 0
110
+
# 1 2 0 2 0 1 0 0
111
+
# 2 4 0 3 0 2 0 1 # dp
112
+
```
113
+
4. According to the `dp` grid data, derive the "recursive formula".
114
+
115
+
```java
116
+
dp[j] = dp[abs(j - nums[i])] + dp[j + nums[i]]
117
+
```
130
118
- If `j < nums[i]`, `dp[j - nums[i]]` will raise `array index out of range` exception. So we use the `dp[abs(j - num)]` which is equal to it, because the `dp[j]` are symmetrical around `0`, such as `dp[-j]` equals to `dp[j]` (`-j` is an imaginary index).
131
-
4. Determine the `dp` array's traversal order
132
-
- `dp[j]` depends on `dp[abs(j - nums[i])]` and `dp[j + nums[i]]`, so we can traverse the `dp` array in any order, but must reference the clone of `dp` to prevent the referenced value from being modified during the iteration.
133
-
- For `j + nums[i] >= dp.length`, `dp[j + nums[i]]` must be `0` because their values are too large and exceed the maximum sum of `nums`.
134
-
5. Check the `dp` array's value
135
-
- Print the `dp` to see if it is as expected.
119
+
- When `j + nums[i] >= dp.length`, `dp[j + nums[i]]` must be `0` to prevent interference.
120
+
5. Write a program and print the `dp` array. If it is not as expected, adjust it.