<filename>programming/leetcode/january_challenge_2021/BeautifulArrangement.py
class Solution:
    count = 0

    def countArrangement(self, n: int) -> int:
        visited = [False for _ in range(0, n + 1)]
        self.calculate(n, 1, visited)
        return self.count

    def calculate(self, n, pos, visited):

        if pos > n:
            self.count += 1

        for i in range(1, n + 1):
            if not visited[i] and (pos % i == 0 or i % pos == 0):
                visited[i] = True
                self.calculate(n, pos + 1, visited)
                visited[i] = False

<filename>programming/leetcode/january_challenge_2021/BeautifulArrangement.py
class Solution:
    count = 0

    def countArrangement(self, n: int) -> int:
        visited = [False for _ in range(0, n + 1)]
        self.calculate(n, 1, visited)
        return self.count

    def calculate(self, n, pos, visited):

        if pos > n:
            self.count += 1

        for i in range(1, n + 1):
            if not visited[i] and (pos % i == 0 or i % pos == 0):
                visited[i] = True
                self.calculate(n, pos + 1, visited)
                visited[i] = False
