<p>You are given an integer array <code>nums</code> and an integer <code>target</code>.</p>

<p>You want to build an <strong>expression</strong> out of nums by adding one of the symbols <code>&#39;+&#39;</code> and <code>&#39;-&#39;</code> before each integer in nums and then concatenate all the integers.</p>

<ul>
	<li>For example, if <code>nums = [2, 1]</code>, you can add a <code>&#39;+&#39;</code> before <code>2</code> and a <code>&#39;-&#39;</code> before <code>1</code> and concatenate them to build the expression <code>&quot;+2-1&quot;</code>.</li>
</ul>

<p>Return the number of different <strong>expressions</strong> that you can build, which evaluates to <code>target</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,1,1,1], target = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> 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
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1], target = 1
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 20</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
	<li><code>0 &lt;= sum(nums[i]) &lt;= 1000</code></li>
	<li><code>-1000 &lt;= target &lt;= 1000</code></li>
</ul>

class Solution:
    def findTargetSumWays(self, nums: List[int], target: int) -> int:
        # Sol 1
        
        if len(nums)==1:
            if abs(nums[0])==abs(target):
                return 1
            return 0
        if sum(nums)&1!=target&1:
            return 0
        zc=0
        idx=0
        while idx<len(nums):
            if nums[idx]==0:
                zc += 1
                nums.pop(idx)
            else:
                idx += 1
        if len(nums)==0:
            if target==0:
                return 1<<zc
            return 0
        pref=[0]*len(nums)
        pref[-1]=nums[-1]
        for i in range(len(nums)-2,-1,-1):
            pref[i]=pref[i+1]+nums[i]
        
        @cache
        def h(idx,cur):
            if idx==len(nums) and cur==target:
                return 1
            if idx==len(nums):
                return 0
            if cur+pref[idx]<target or cur-pref[idx]>target:
                return 0
            return h(idx+1,cur+nums[idx]) + h(idx+1,cur-nums[idx])
        
        return h(0,0)*(1<<zc)
        
        if len(nums)==1:
            if abs(nums[0])==abs(target):
                return 1
            return 0
        if sum(nums)&1!=target&1:
            return 0
        zc=0
        idx=0
        while idx<len(nums):
            if nums[idx]==0:
                zc += 1
                nums.pop(idx)
            else:
                idx += 1
        if len(nums)==0:
            if target==0:
                return 1<<zc
            return 0
        
        dp=[0]*((target<<1)+1)
        dp[target]=1
        for n in nums:
            nxt=list(dp)
            for i in range(n,len(dp)-n):
                nxt[i] += dp[i-n]+dp[i+n]
            for i in range(n):
                nxt[i] += dp[i+n]
            for i in range(len(dp)-n,len(dp)):
                nxt[i] += dp[i-n]
            dp=list(nxt)
        print(dp)
        return dp[-1]*(1<<zc)
                
19501