> ## 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 IV Python Solution with Tests

> Tested Python solution for LeetCode 377 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 377, Medium. Topics: Array, Dynamic Programming. [View on LeetCode](https://leetcode.com/problems/combination-sum-iv/description/).

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

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

## Problem

Given an array of **distinct** integers `nums` and a target integer `target`, return the number of possible combinations that add up to `target`.

The test cases are generated so that the answer can fit in a 32-bit integer.

Note that different sequences are counted as different combinations.

### Examples

```
Input: nums = [1,2,3], target = 4
Output: 7
Explanation:
The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Note that different sequences are counted as different combinations.
```

```
Input: nums = [9], target = 3
Output: 0
```

### Constraints

* 1 \<= nums.length \<= 200
* 1 \<= nums\[i] \<= 1000
* All the elements of `nums` are **unique**.
* 1 \<= target \<= 1000

**Follow up:** What if negative numbers are allowed in the given array? How does it change the problem? What limitation we need to add to the question to allow negative numbers?

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Bottom-up DP: dp[t] = number of ways to reach sum t
    # dp[t] = sum(dp[t - num] for num in nums if t >= num), dp[0] = 1
    # Order matters, so iterate target outer, nums inner
    # Time: O(target * n)
    # Space: O(target)
    def combination_sum4(self, nums: list[int], target: int) -> int:
        dp = [0] * (target + 1)
        dp[0] = 1
        for t in range(1, target + 1):
            for num in nums:
                if t >= num:
                    dp[t] += dp[t - num]
        return dp[target]
```

## Complexity

| Time           | Space     |
| -------------- | --------- |
| O(target \* n) | O(target) |

## Tags

[Grind](/catalog/grind), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
