1010"""
1111# Solution using Backtracking
1212
13- class beautifulArrange :
13+ class beautifularrange :
1414 # funtion call; n is the size of the permutation (numbers 1..n)
15- def countArrangement (self , n : int ) -> int :
15+ def countarrangement (self , n : int ) -> int :
1616
1717 self .count = 0
1818 """
1919 We initialize a counter to record how many valid arrangements we find.
20- Using self.count lets the nested function modify it without nonlocal."""
20+ Using self.count lets the nested function modify it without nonlocal.
21+ """
2122
2223 used = [False ] * (n + 1 )
2324 """
24- A boolean list to mark which numbers have already been placed in the permutation.
25+ A boolean list to mark which numbers have
26+ already been placed in the permutation.
2527 """
2628
2729 def backtrack (pos ):
@@ -32,16 +34,19 @@ def backtrack(pos):
3234 """
3335 if pos > n :
3436
35- self .count += 1 # We found a complete valid arrangement, so increment the total count.
37+ self .count += 1
38+ # We found a complete valid arrangement, so increment the total count.
3639 return
37- for num in range (1 , n + 1 ): # Try every candidate number num for the current position pos.
40+ for num in range (1 , n + 1 ):
41+ # Try every candidate number num for the current position pos.
3842
3943
4044 """
4145 Two checks in one:
4246 1. not used[num] — the number num has not been placed yet (we can use it).
43- 2. (num % pos == 0 or pos % num == 0) — the beautiful-arrangement condition:
44- number num is compatible with position pos (either num divides pos or pos divides num).
47+ 2. (num % pos == 0 or pos % num == 0) — the
48+ beautiful-arrangement condition:
49+ either num divides pos or pos divides num.
4550 If both are true, num is a valid choice for pos.
4651
4752 """
0 commit comments