> ## 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.

# Target Sum Python Solution with Tests

> Tested Python solution for LeetCode 494 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 494, Medium. Topics: Array, Dynamic Programming, Backtracking. [View on LeetCode](https://leetcode.com/problems/target-sum/description/).

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

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

## Problem

You are given an integer array `nums` and an integer `target`.

You want to build an expression out of nums by adding one of the symbols `+` and `-` before each integer in nums and then concatenate all the integers.

* For example, if `nums = [2, 1]`, you can add a `+` before `2` and a `-` before `1` and concatenate them to build the expression `+2-1`.

Return *the number of different expressions that you can build, which evaluates to* `target`.

### Examples

```
Input: nums = [1,1,1,1,1], target = 3
Output: 5
```

**Explanation:** There are 5 ways to assign symbols to make the sum of nums be target 3.
-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3

```
Input: nums = [1], target = 1
Output: 1
```

### Constraints

* 1 \<= nums.length \<= 20
* 0 \<= nums\[i] \<= 1000
* 0 \<= sum(nums\[i]) \<= 1000
* -1000 \<= target \<= 1000

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * total)
    # Space: O(total)
    def find_target_sum_ways(self, nums: list[int], target: int) -> int:
        total = sum(nums)
        # P = sum of '+'. P - (total - P) = target -> P = (target + total) / 2.
        if (target + total) % 2 != 0 or total < abs(target):
            return 0
        subset_sum = (target + total) // 2

        # dp[s] = number of subsets summing to s
        dp = [0] * (subset_sum + 1)
        dp[0] = 1
        for num in nums:
            for s in range(subset_sum, num - 1, -1):
                dp[s] += dp[s - num]
        return dp[subset_sum]
```

## Complexity

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

## Tags

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