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: dsa-solutions/lc-solutions/0000-0099/0040-Combination-sum-II.md
+15-15Lines changed: 15 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -66,44 +66,44 @@ The solution uses depth-first search (DFS), backtracking, and sorting to effecti
66
66
67
67
1.**Sorting the Candidates:** The candidates array is first sorted to ensure that we can easily skip duplicates.
68
68
69
-
- candidates.sort()
69
+
-`candidates.sort()`
70
70
71
71
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).
72
72
73
-
- def dfs(i: int, s: int):
73
+
-`def dfs(i: int, s: int):`
74
74
75
75
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.
76
76
77
-
- if s == 0:
78
-
- ans.append(t[:])
77
+
-`if s == 0:`
78
+
-`ans.append(t[:])`
79
79
80
80
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.
81
81
82
-
- if i >= len(candidates) or s < candidates[i]:
82
+
-`if i >= len(candidates) or s < candidates[i]:`
83
83
-return
84
84
85
85
5.**Loop Over the Candidates:** Starting from the current index to the end of candidates, we try to include the candidate in the combination:
86
86
87
-
- for j in range(i, len(candidates)):
87
+
-`for j in range(i, len(candidates)):`
88
88
89
89
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).
90
90
91
-
- if j > i and candidates[j] == candidates[j - 1]:
91
+
-`if j > i and candidates[j] == candidates[j - 1]:`
92
92
- continue
93
93
94
94
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.
95
95
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()`
99
99
100
100
The solution utilizes a list ans to store all the unique combinations and a temporary list t to store the current combination being constructed.
101
101
102
102
After defining dfs, the solution begins the search with:
103
103
104
-
1. ans = []
105
-
2. t = []
106
-
3. dfs(0, target)
104
+
1.`ans = []`
105
+
2.`t = []`
106
+
3.`dfs(0, target)`
107
107
108
108
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.
109
109
@@ -317,8 +317,8 @@ The space complexity of the code consists of:
317
317
318
318
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.
319
319
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)$.
321
321
322
322
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)$.
323
323
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