mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-15 16:36:06 +00:00
build
This commit is contained in:
@@ -15,11 +15,11 @@ For example, given the set $\{3, 4, 5\}$ and target integer $9$, the solutions a
|
||||
- Elements in the input set can be selected repeatedly without limit.
|
||||
- Subsets do not distinguish element order; for example, $\{4, 5\}$ and $\{5, 4\}$ are the same subset.
|
||||
|
||||
### 1. Reference to Full Permutation Solution
|
||||
### 1. Using the Permutation Solution as a Reference
|
||||
|
||||
Similar to the full permutation problem, we can imagine the process of generating subsets as a series of choices, and update the "sum of elements" in real-time during the selection process. When the sum equals `target`, we record the subset to the result list.
|
||||
Similar to the permutation problem, we can view the process of generating subsets as the result of a series of choices and update the running sum during the selection process. When the sum equals `target`, we record the subset in the result list.
|
||||
|
||||
Unlike the full permutation problem, **elements in this problem's set can be selected unlimited times**, so we do not need to use a `selected` boolean list to track whether an element has been selected. We can make minor modifications to the full permutation code and initially obtain the solution:
|
||||
Unlike the permutation problem, **elements in this problem can be selected any number of times**, so we do not need to use a `selected` boolean list to track whether an element has already been selected. With a few small changes to the permutation code, we obtain an initial solution:
|
||||
|
||||
=== "Python"
|
||||
|
||||
@@ -501,7 +501,7 @@ Unlike the full permutation problem, **elements in this problem's set can be sel
|
||||
end
|
||||
```
|
||||
|
||||
When we input array $[3, 4, 5]$ and target element $9$ to the above code, the output is $[3, 3, 3], [4, 5], [5, 4]$. **Although we successfully find all subsets that sum to $9$, there are duplicate subsets $[4, 5]$ and $[5, 4]$**.
|
||||
Running the above code on array $[3, 4, 5]$ with target value $9$ produces $[3, 3, 3], [4, 5], [5, 4]$. **Although we successfully found all subsets that sum to $9$, there are duplicate subsets $[4, 5]$ and $[5, 4]$**.
|
||||
|
||||
This is because the search process distinguishes the order of selections, but subsets do not distinguish selection order. As shown in Figure 13-10, selecting 4 first and then 5 versus selecting 5 first and then 4 are different branches, but they correspond to the same subset.
|
||||
|
||||
@@ -519,13 +519,13 @@ To eliminate duplicate subsets, **one straightforward idea is to deduplicate the
|
||||
**We consider deduplication through pruning during the search process**. Observing Figure 13-11, duplicate subsets occur when array elements are selected in different orders, as in the following cases:
|
||||
|
||||
1. When the first and second rounds select $3$ and $4$ respectively, all subsets containing these two elements are generated, denoted as $[3, 4, \dots]$.
|
||||
2. Afterward, when the first round selects $4$, **the second round should skip $3$**, because the subset $[4, 3, \dots]$ generated by this choice is completely duplicate with the subset generated in step `1.`
|
||||
2. Afterward, when the first round selects $4$, **the second round should skip $3$**, because the subset $[4, 3, \dots]$ generated by this choice is an exact duplicate of the subset generated in step `1.`
|
||||
|
||||
In the search process, each level's choices are tried from left to right, so the rightmost branches are pruned more.
|
||||
|
||||
1. The first two rounds select $3$ and $5$, generating subset $[3, 5, \dots]$.
|
||||
2. The first two rounds select $4$ and $5$, generating subset $[4, 5, \dots]$.
|
||||
3. If the first round selects $5$, **the second round should skip $3$ and $4$**, because subsets $[5, 3, \dots]$ and $[5, 4, \dots]$ are completely duplicate with the subsets described in steps `1.` and `2.`
|
||||
3. If the first round selects $5$, **the second round should skip $3$ and $4$**, because subsets $[5, 3, \dots]$ and $[5, 4, \dots]$ are exact duplicates of the subsets described in steps `1.` and `2.`
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -1057,7 +1057,7 @@ In addition, we have made the following two optimizations to the code:
|
||||
end
|
||||
```
|
||||
|
||||
Figure 13-12 shows the complete backtracking process when array $[3, 4, 5]$ and target element $9$ are input to the above code.
|
||||
Figure 13-12 shows the complete backtracking process produced by running the above code on array $[3, 4, 5]$ with target value $9$.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@@ -1069,7 +1069,7 @@ Figure 13-12 shows the complete backtracking process when array $[3, 4, 5]$ and
|
||||
|
||||
Given a positive integer array `nums` and a target positive integer `target`, find all possible combinations where the sum of elements in the combination equals `target`. **The given array may contain duplicate elements, and each element can be selected at most once**. Return these combinations in list form, where the list should not contain duplicate combinations.
|
||||
|
||||
Compared to the previous problem, **the input array in this problem may contain duplicate elements**, which introduces new challenges. For example, given array $[4, \hat{4}, 5]$ and target element $9$, the output of the existing code is $[4, 5], [\hat{4}, 5]$, which contains duplicate subsets.
|
||||
Compared to the previous problem, **the input array in this problem may contain duplicate elements**, which introduces a new issue. For example, given array $[4, \hat{4}, 5]$ and target value $9$, the output of the existing code is $[4, 5], [\hat{4}, 5]$, which contains duplicate subsets.
|
||||
|
||||
**The reason for this duplication is that equal elements are selected multiple times in a certain round**. In Figure 13-13, the first round has three choices, two of which are $4$, creating two duplicate search branches that output duplicate subsets. Similarly, the two $4$'s in the second round also produce duplicate subsets.
|
||||
|
||||
@@ -1079,7 +1079,7 @@ Compared to the previous problem, **the input array in this problem may contain
|
||||
|
||||
### 1. Pruning Equal Elements
|
||||
|
||||
To solve this problem, **we need to limit equal elements to be selected only once in each round**. The implementation is quite clever: since the array is already sorted, equal elements are adjacent. This means that in a certain round of selection, if the current element equals the element to its left, it means this element has already been selected, so we skip the current element directly.
|
||||
To solve this problem, **we need to limit equal elements to be selected only once in each round**. The implementation is quite clever: since the array is already sorted, equal elements are adjacent. This means that in a given round of selection, if the current element equals the element to its left, then the same value has already been chosen in this round, so we skip the current element directly.
|
||||
|
||||
At the same time, **this problem specifies that each array element can only be selected once**. Fortunately, we can also use the variable `start` to satisfy this constraint: after making choice $x_{i}$, set the next round to start traversal from index $i + 1$ onwards. This both eliminates duplicate subsets and avoids selecting elements multiple times.
|
||||
|
||||
@@ -1663,7 +1663,7 @@ At the same time, **this problem specifies that each array element can only be s
|
||||
end
|
||||
```
|
||||
|
||||
Figure 13-14 shows the backtracking process for array $[4, 4, 5]$ and target element $9$, which includes four types of pruning operations. Combine the illustration with the code comments to understand the entire search process and how each pruning operation works.
|
||||
Figure 13-14 shows the backtracking process for array $[4, 4, 5]$ with target value $9$, which includes four types of pruning operations. Combine the illustration with the code comments to understand the entire search process and how each pruning operation works.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user