Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdecfe8e

Browse files
committed
Update all yesterday solution changes.
1 parent25cefb5 commitdecfe8e

File tree

44 files changed

+577
-474
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+577
-474
lines changed

‎en/1-1000/416-partition-equal-subset-sum.md

Lines changed: 45 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,20 @@ Given an integer array `nums`, return `true` if you can partition the array into
3838
##Intuition 1
3939

4040
- 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.
4442

4543
##Steps
4644

47-
###Common steps in '0/1 Knapsack Problem'
48-
These five steps are a pattern for solving`Dynamic Programming` problems.
49-
5045
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`.
5447
-`dp[j]` is a boolean.
5548

5649
2. Determine the`dp` array's initial value
5750
- Use an example:
5851

5952
```
6053
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`.
6255
So after initialization, the 'dp' array would be:
6356
# 0 1 2 3 4 5 6 7 8 9 10 11
6457
# 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.
6760
# 11
6861
# 5
6962
```
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.
7163
- `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`.
7264
- `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.
121107
122108
## Complexity
123109

‎en/1-1000/474-ones-and-zeroes.md

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,9 @@ This question is difficult. It is recommended to complete a simple question of t
5353

5454
##Steps
5555

56-
###Common steps in '0/1 Knapsack Problem'
57-
These five steps are a pattern for solving`Dynamic Programming` problems.
58-
5956
1. Determine the**meaning** of the`dp[j]`
6057
- 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`.
6159
-`dp[j]` represents the maximum number of strings that can be selected with at most`j` zeros.
6260
-`dp[j]` is an integer.
6361

@@ -68,11 +66,10 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
6866
max_zero_count= m
6967
dp= [0]* (max_zero_count+1)
7068
```
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.
7269
-`dp[0] =0`, indicating thatwith no zeros, we can select0 strings.
7370
-`dp[j] =0`as the initial value because we will use`max` to increase them later.
7471

75-
3.Determinethe`dp`array's recurrence formula
72+
3.According to an example, fillinthe`dp`grid data"in order".
7673
- Let's analyze the example step by step:
7774

7875
```
@@ -100,25 +97,13 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
10097
# 0 1 2 3 4 5
10198
# 0 2 3 3 4 4
10299
```
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".
111101

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:
122107

123108
```python
124109
classSolution:
@@ -144,7 +129,7 @@ These five steps are a pattern for solving `Dynamic Programming` problems.
144129
return zero_count
145130
```
146131

147-
### Now, you can consider another dimension: the quantity limit of `1`.
132+
#### Now, you can consider another dimension: the quantity limit of `1`.
148133

149134
It should be handled similarly to`0` butin another dimension. Please see the complete code below.
150135

‎en/1-1000/494-target-sum.md

Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,14 @@ Return the number of different **expressions** that you can build, which evaluat
4646

4747
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.
4848

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-
5449
##Steps
5550

56-
###Common steps in '0/1 Knapsack Problem'
57-
These five steps are a pattern for solving`Dynamic Programming` problems.
58-
5951
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`.
6353
-`dp[j]` is an**integer**.
6454
2. Determine the`dp` array's initial value
6555
- 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`.
6757
- First, determine the`size` of the knapsack.
6858
- The`target` value may be very small, such as`0`, so it alone cannot determine the`size` of the knapsack.
6959
- 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.
7363

7464
```
7565
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
7868
# 1
7969
# 2
8070
# 1
8171
# 2
8272
```
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.
8473
- `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`.
8574
- `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+
```
130118
- 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.
136121
137122
## Complexity
138123

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp