Python simple DFS solution

  • 0

    Python simple DFS solution, inspired by common topics like subset, combinations...

    class Solution(object):
        def countArrangement(self, N):
            :type N: int
            :rtype: int
            if not N or N < 1:
                return 0
            visited = [False]*N
            count = [0]
            self.helper(visited, N, 0, count)
            return count[0]
        def helper(self, visited, N, i, count):
            if i == N:
                count[0] += 1
            for n in range(N):
                divisible = ((n+1) % (i+1) == 0) or ((i+1) % (n+1)== 0)
                if not visited[n] and divisible:
                    visited[n] = True
                    self.helper(visited, N, i+1, count)
                    visited[n] = False

Log in to reply

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