Beautiful Arrangement LeetCode solution in Python
1class Solution:2def countArrangement(self, n: int) -> int:3self.count = 04self.backtrack(n, 1, [])5return self.count67def backtrack(self, N, idx, temp):8if len(temp) == N:9self.count += 110return1112for i in range(1, N + 1):13if i not in temp and (i % idx == 0 or idx % i == 0):14temp.append(i)15self.backtrack(N, idx + 1, temp)16temp.pop()