Python Dynamic


  • 0

    Follow Up: if negative numbers are allowed in the given array, then there will be potential of Dead circulation, like target is 0 and given [-1, 1], there are infinite comination if duplicates element are allow to be count.

    class Solution(object):
        def combinationSum4(self, nums, target):
           cntfortarget = [1] + [0 for i in xrange(target)]
            for i in xrange(1, target + 1):
                for num in nums:
                    if i - num >= 0:
                        cntfortarget[i] += cntfortarget[i - num]
            return cntfortarget[-1]
    

Log in to reply
 

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