Python Solution With Dp


  • 0
    B
    class Solution(object):
        def countArrangement(self, N):
            """
            :type N: int
            :rtype: int
            """
            if N is 0:
                return 0
            def helper(num,state=()):
                if num == 1:
                    return 1
                key = num,state
                if key in cache:
                    return cache[key]
                generator = ( helper(num - 1, state[:j] + state[j + 1:]) for j, x in enumerate(state) if x % num == 0 or num % x == 0)
                cache[key] = sum(generator)
                return cache[key]
            return helper(N,tuple(range(1,N+1)))

Log in to reply
 

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