#
# @lc app=leetcode id=494 lang=python3
#
# [494] Target Sum
#

# @lc code=start
class Solution:
    def findTargetSumWays(self, nums: List[int], S: int) -> int:
        dp = {0: 1}

        for n in nums:
            nxtDp = {}
            for cur in dp:
                nxtDp[cur + n] = nxtDp.get(cur + n, 0) + dp[cur]
                nxtDp[cur - n] = nxtDp.get(cur - n, 0) + dp[cur]
            dp = nxtDp
        return dp.get(S, 0)

        
# @lc code=end


da4b7154e3780c37ffbb5bb437216c71f30fc466
<mask token>

class Solution:
    <mask token>

class Solution:

    def findTargetSumWays(self, nums: List[int], S: int) ->int:
        dp = {(0): 1}
        for n in nums:
            nxtDp = {}
            for cur in dp:
                nxtDp[cur + n] = nxtDp.get(cur + n, 0) + dp[cur]
                nxtDp[cur - n] = nxtDp.get(cur - n, 0) + dp[cur]
            dp = nxtDp
        return dp.get(S, 0)

#
# @lc app=leetcode id=494 lang=python3
#
# [494] Target Sum
#

# @lc code=start
class Solution:
    def findTargetSumWays(self, nums: List[int], S: int) -> int:
        dp = {0: 1}

        for n in nums:
            nxtDp = {}
            for cur in dp:
                nxtDp[cur + n] = nxtDp.get(cur + n, 0) + dp[cur]
                nxtDp[cur - n] = nxtDp.get(cur - n, 0) + dp[cur]
            dp = nxtDp
        return dp.get(S, 0)

        
# @lc code=end

