> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Combination Sum III Python Solution with Tests

> Tested Python solution for LeetCode 216 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 216, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Backtracking](/catalog/topics/backtracking). [View on LeetCode](https://leetcode.com/problems/combination-sum-iii/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 216   # by problem number
lcpy gen -s combination_sum_iii   # by problem name
```

## Problem

Find all valid combinations of `k` numbers that sum up to `n` such that the following conditions are true:

* Only numbers `1` through `9` are 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.

### Examples

```
Input: k = 3, n = 7
Output: [[1,2,4]]
Explanation:
1 + 2 + 4 = 7
There are no other valid combinations.
```

```
Input: k = 3, n = 9
Output: [[1,2,6],[1,3,5],[2,3,4]]
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.
```

```
Input: k = 4, n = 1
Output: []
Explanation: There are no valid combinations.
Using 4 different numbers in the range [1,9], the smallest sum we can get is 1+2+3+4 = 10 and since 10 > 1, there are no valid combination.
```

### Constraints

* 2 \<= k \<= 9
* 1 \<= n \<= 60

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/combination_sum_iii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/combination_sum_iii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(C(9, k) * k)
    # Space: O(k) recursion depth (output excluded)
    def combination_sum_3(self, k: int, n: int) -> list[list[int]]:
        results: list[list[int]] = []
        combo: list[int] = []

        def backtrack(start: int, remaining: int) -> None:
            if len(combo) == k:
                if remaining == 0:
                    results.append([*combo])
                return
            for num in range(start, 10):
                if num > remaining:
                    break
                combo.append(num)
                backtrack(num + 1, remaining - num)
                combo.pop()

        backtrack(1, n)
        return results
```

## Complexity

| Time | Space |
| - | - |
| O(C(9, k) \* k) | O(k) recursion depth (output excluded) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.