Combinations
Return all combinations of k numbers chosen from 1 through n using backtracking with forward-looking pruning.
01The problem
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.
| Input | Output | Why |
|---|---|---|
n = 4, k = 2 | [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]] | All 6 pairs chosen from {1, 2, 3, 4} |
n = 1, k = 1 | [[1]] | Single choice |
Constraints: 1 <= n <= 20, 1 <= k <= n.
Loading the rest of the lesson…