Iterative DP with Counter


  • 0
    B
    class Solution(object):
        def findTargetSumWays(self, nums, S):
            counts = collections.Counter({0:1})
            for num in nums:
                new = collections.Counter()
                for k in counts:
                    new[k-num] += counts[k]
                    new[k+num] += counts[k]
                counts = new
            return counts[S]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.