Skip to content

Commit 0c78e7f

Browse files
corrected
1 parent b30dd09 commit 0c78e7f

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

dsa-solutions/lc-solutions/0000-0099/0040-Combination-sum-II.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,44 +66,44 @@ The solution uses depth-first search (DFS), backtracking, and sorting to effecti
6666

6767
1. **Sorting the Candidates:** The candidates array is first sorted to ensure that we can easily skip duplicates.
6868

69-
- candidates.sort()
69+
- `candidates.sort()`
7070

7171
2. **Depth-First Search (DFS) Function:** The dfs function is defined to handle the recursion, taking two parameters: i (the current index in the candidates list) and s (the remaining sum needed to reach the target).
7272

73-
- def dfs(i: int, s: int):
73+
- `def dfs(i: int, s: int):`
7474

7575
3. **Condition to Add to the Answer:** Inside the dfs function, we check if the remaining sum s is zero, meaning we found a valid combination that sums to the target. In that case, we add a copy of the current combination to the answer list.
7676

77-
- if s == 0:
78-
- ans.append(t[:])
77+
- `if s == 0:`
78+
- `ans.append(t[:])`
7979

8080
4. **Base Cases for Termination:** We return if the index i moves beyond the length of the candidates or if the remaining sum s is less than the current candidate, which means we can't reach the desired total with the current and subsequent candidates since they are all larger.
8181

82-
- if i >= len(candidates) or s < candidates[i]:
82+
- `if i >= len(candidates) or s < candidates[i]:`
8383
- return
8484

8585
5. **Loop Over the Candidates:** Starting from the current index to the end of candidates, we try to include the candidate in the combination:
8686

87-
- for j in range(i, len(candidates)):
87+
- `for j in range(i, len(candidates)):`
8888

8989
6. **Skipping Duplicates:** Before including a candidate in the combination, we skip over it if it's the same as its predecessor to avoid duplicates in our answer list (since we’ve already considered this value in the previous steps).
9090

91-
- if j > i and candidates[j] == candidates[j - 1]:
91+
- `if j > i and candidates[j] == candidates[j - 1]:`
9292
- continue
9393

9494
7. **Backtracking:** After including a candidate, the dfs function is called recursively with the updated index (j+1) and the updated remaining sum (s - candidates[j]). After this recursive call, we backtrack by removing the last candidate from the combination and moving on to the next candidate.
9595

96-
- t.append(candidates[j])
97-
- dfs(j + 1, s - candidates[j])
98-
- t.pop()
96+
- `t.append(candidates[j])`
97+
- `dfs(j + 1, s - candidates[j])`
98+
- `t.pop()`
9999

100100
The solution utilizes a list ans to store all the unique combinations and a temporary list t to store the current combination being constructed.
101101

102102
After defining dfs, the solution begins the search with:
103103

104-
1. ans = []
105-
2. t = []
106-
3. dfs(0, target)
104+
1. `ans = []`
105+
2. `t = []`
106+
3. `dfs(0, target)`
107107

108108
The DFS and backtracking continue until all possible combinations that meet the criteria have been explored, after which ans is returned, containing all the valid combinations.
109109

@@ -317,8 +317,8 @@ The space complexity of the code consists of:
317317

318318
1. Space used by the recursion stack, which in the worst case is equivalent to the depth of recursion, at most $O(n)$ if all candidates are used.
319319

320-
2. Space for the temporary list t, which stores the current combination, will at most contain n values, adding another O(n).
320+
2. Space for the temporary list t, which stores the current combination, will at most contain n values, adding another $O(n)$.
321321

322322
3. Finally, the output list ans that could potentially hold all unique combinations of candidates. In the worst case, this could be all possible combinations which can be exponential, represented as $O(2^n)$.
323323

324-
4. Hence, the overall space complexity, considering the output space and the recursion stack depth, is O(n + 2^n). Typically, the output space can be a separate consideration, and if we exclude it, the space complexity for computation is $O(n)$. However, if we include the space needed for the output, it would be $O(2^n)$ due to the possibility of storing all combinations.
324+
4. Hence, the overall space complexity, considering the output space and the recursion stack depth, is $O(n + 2^n)$. Typically, the output space can be a separate consideration, and if we exclude it, the space complexity for computation is $O(n)$. However, if we include the space needed for the output, it would be $O(2^n)$ due to the possibility of storing all combinations.

0 commit comments

Comments
 (0)