You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.
 

Find out how many ways to assign symbols to make sum of integers equal to target S.  


Example 1:

Input: nums is [1, 1, 1, 1, 1], S is 3. 
Output: 5
Explanation: 

-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

There are 5 ways to assign symbols to make the sum of nums be target 3.



Note:

The length of the given array is positive and will not exceed 20. 
The sum of elements in the given array will not exceed 1000.
Your output answer is guaranteed to be fitted in a 32-bit integer.

Difficulty: interview

Starter code:

class Solution:
    def findTargetSumWays(self, nums: List[int], S: int) -> int:
        

Solutions:
class Solution:
     def findTargetSumWays(self, nums, S):
         """
         :type nums: List[int]
         :type S: int
         :rtype: int
         """
         c = [0]*1001
         c[0] = 1
         T = sum(nums)
         A = T+S
         if T<S or A&1:
             return 0
         A>>=1
         nums = sorted(nums)
         temp = 0
         for ind, v in enumerate(nums):
             temp += v
             for i in range(min(temp, A), v-1, -1):
                 c[i] += c[i-v]
         return c[A]

class Solution:
     def findTargetSumWays(self, nums, S):
         """
         :type nums: List[int]
         :type S: int
         :rtype: int
         """
         total = sum(nums)
         if total < S or (total + S) & 1:
             return 0
         target = (S + total) // 2
         dp = [0] * (target + 1)
         dp[0] = 1
         for n in nums:
             for i in range(target, n-1, -1):
                 dp[i] += dp[i - n]
         return dp[target]

class Solution:
     def findTargetSumWays(self, nums, S):
         """
         :type nums: List[int]
         :type S: int
         :rtype: int
         """
         def findTargetNumber(nums, target):
             print(nums, target)
             dp = [0 for _ in range(target + 1)]
             dp[0] = 1
             for n in nums:
                 for i in range(target, n - 1, -1):
                     dp[i] += dp[i - n]
             return dp[-1]
         
         sumResult = sum(nums)
         if (S + sumResult) % 2 != 0 or sumResult < S:
             return 0
         else:
             return findTargetNumber(nums, int((sumResult + S) / 2))

class Solution:
     def findTargetSumWays(self, nums, S):
         sum_ = sum(nums)
         if S > sum_: return 0
         target = sum_ - S
         if target % 2: return 0
         target = target // 2
 
         dp = [1] + [0] * target
         for n in nums:
             i = target  
             while(i>=n):  
                 dp[i] += dp[i-n]  
                 i -= 1 
         return dp[target]
         


class Solution:
     def findTargetSumWays(self, nums, S):
         """
         :type nums: List[int]
         :type S: int
         :rtype: int
         """
         if sum(nums)<S:  
             return 0  
         if (S+sum(nums))%2==1:  
             return 0  
         target = (S+sum(nums))//2  
         dp = [0]*(target+1)  
         dp[0] = 1  
         for n in nums:  
             i = target  
             while(i>=n):  
                 dp[i] = dp[i] + dp[i-n]  
                 i = i-1  
         return dp[target] 


class Solution:
     def findTargetSumWays(self, nums, S):
         """
         :type nums: List[int]
         :type S: int
         :rtype: int
         """
         # A: postive set. B: negative set
         # A - B = S
         # A + B = sum(nums)
         # => A = (S + sum(nums)) / 2
         target = S + sum(nums)
         if target % 2 == 1 or sum(nums) < S:
             return 0
         target //= 2
         # 0-1 knapsack
         dp = [0 for i in range(target + 1)]
         dp[0] = 1
         for curNum in nums:
             for cap in range(len(dp) - 1, -1, -1):
                 if cap >= curNum:
                     dp[cap] += dp[cap - curNum]
         return dp[-1]