Combinations⚓︎
Description⚓︎
Given two integers n
and k
, return all possible combinations of k
numbers chosen from the range [1, n]
.
You may return the answer in any order.
Example 1:
- Input:
n = 4, k = 2
- Output:
[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
- Explanation: There are
4 choose 2 = 6
total combinations. Note that combinations are unordered, i.e.,[1,2]
and[2,1]
are considered to be the same combination.
Example 2:
- Input:
n = 1, k = 1
- Output:
[[1]]
- Explanation: There is
1 choose 1 = 1
total combination.
Constraints:
1 <= n <= 20
1 <= k <= n
Solution⚓︎
Backtracking Algorithm (Recursive)⚓︎
Template pseudocode:
Original Solution:
Solution after pruning:
Code with comments:
Algorithm Steps:
- Initialization: The class maintains two vectors,
res
for storing all combinations andpath
for the current combination being explored. - Backtracking Function (
backtrack
):- Base Case: If the size of
path
is equal tok
, the desired combination size, the current combination is added tores
. - Recursive Case: The function iterates from
startIndex
ton - (k - path.size()) + 1
. This iteration limit ensures that there are enough elements left to complete the combination. For each iteration:- Add the current element
i
topath
. - Recursively call
backtrack
with the next starting index (i + 1
). - Perform backtracking by removing the last element from
path
to explore other combinations.
- Add the current element
- Base Case: If the size of
- Generating Combinations: The public
combine
function initiates the backtracking process starting from index1
.
Backtracking is a DFS (depth-first search) approach to explore all possible combinations. In this problem, backtracking helps in generating all combinations of size k
from numbers 1
to n
. Whenever a combination of size k
is formed, it is added to the result. The algorithm then backtracks to replace the last element with the next possible number and continues this process until all combinations are explored.
Time Complexity:
- Worst Case: \(O\left( C_{n}^{k}\times k \right)\)
- There are \(C_{n}^{k}\) possible combinations.
- For each combination, it takes \(O(k)\) time to copy the
path
vector tores
.
Space Complexity:
- \(O\left( n+k \right) \approx O\left( n \right)\).
Backtracking Algorithm (Iterative)⚓︎
See reference.
Code with comments:
Simulation result:
Explanation:
- Initialization:
res
is used to store the resulting combinations.index
is the position in the current combination being processed, andpath
is a vector of sizek
, initially filled with zeros. - Iteration: The
while
loop continues as long asindex
is non-negative. - Incrementing Elements:
path[index]++
increments the element at the current position. - Backtracking: If
path[index]
becomes greater thann
, it means we need to backtrack, i.e., move back to the previous position (index--
). - Adding a Combination: If
index
isk - 1
and the current element is valid (≤n
), a complete combination is formed and added tores
. - Moving Forward: If neither of the above conditions is met, the algorithm moves to the next position (
index++
) and initializespath[index]
topath[index - 1]
.