Combination Sum III
Find all valid combinations of k distinct numbers from 1 to 9 that sum up to n using constrained backtracking.
01The problem
Find all valid combinations of k numbers that sum up to n such that the following conditions are true:
- Only numbers
1through9are used. - Each number is used at most once.
Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.
| Input | Output | Why |
|---|---|---|
k = 3, n = 7 | [[1, 2, 4]] | 1+2+4 = 7; only valid combination |
k = 3, n = 9 | [[1, 2, 6], [1, 3, 5], [2, 3, 4]] | Three distinct 3-number combinations summing to 9 |
k = 4, n = 1 | [] | Minimum sum of 4 numbers is 1+2+3+4 = 10 > 1 |
Constraints: 2 <= k <= 9, 1 <= n <= 60.
Loading the rest of the lesson…