Beautiful Arrangement LeetCode solution in Python

1
class Solution:
2
def countArrangement(self, n: int) -> int:
3
self.count = 0
4
self.backtrack(n, 1, [])
5
return self.count
6

7
def backtrack(self, N, idx, temp):
8
if len(temp) == N:
9
self.count += 1
10
return
11

12
for i in range(1, N + 1):
13
if i not in temp and (i % idx == 0 or idx % i == 0):
14
temp.append(i)
15
self.backtrack(N, idx + 1, temp)
16
temp.pop()

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0