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 = 6total 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 = 1total combination.
Constraints:
1 <= n <= 201 <= k <= n
Solution⚓︎
Backtracking Algorithm (Recursive)⚓︎
Template pseudocode:
Original Solution:
Solution after pruning:
Code with comments:
Algorithm Steps:
- Initialization: The class maintains two vectors,
resfor storing all combinations andpathfor the current combination being explored. - Backtracking Function (
backtrack):- Base Case: If the size of
pathis equal tok, the desired combination size, the current combination is added tores. - Recursive Case: The function iterates from
startIndexton - (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
itopath. - Recursively call
backtrackwith the next starting index (i + 1). - Perform backtracking by removing the last element from
pathto explore other combinations.
- Add the current element
- Base Case: If the size of
- Generating Combinations: The public
combinefunction 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
pathvector 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:
resis used to store the resulting combinations.indexis the position in the current combination being processed, andpathis a vector of sizek, initially filled with zeros. - Iteration: The
whileloop continues as long asindexis 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
indexisk - 1and 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].