Skip to content

Commit c2c10ee

Browse files
committed
Merge branch 'master' of https://github.com/zirea3l/Python
2 parents 5e0a91d + d96f565 commit c2c10ee

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

dynamic_programming/beautiful_arrangement.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""
2-
Suppose you have n integers labeled 1 through n.
2+
Suppose you have n integers labeled 1 through n.
33
A permutation of those n integers
4-
perm (1-indexed) is considered a
4+
perm (1-indexed) is considered a
55
"beautiful arrangement" if for every i (1 <= i <= n),
66
either of the following is true:
77
88
-> perm[i] is divisible by i.
99
-> i is divisible by perm[i].
10-
Given an integer n, return the number of the
10+
Given an integer n, return the number of the
1111
"beautiful arrangements" that you can construct.
1212
1313
"""
@@ -19,9 +19,9 @@ class BeautifulArrange:
1919
def countarrangement(self, n: int) -> int:
2020
self.count = 0
2121
"""
22-
We initialize a counter to record how
22+
We initialize a counter to record how
2323
many valid arrangements we find.
24-
Using self.count lets the nested
24+
Using self.count lets the nested
2525
function modify it without nonlocal.
2626
"""
2727

@@ -34,7 +34,7 @@ def countarrangement(self, n: int) -> int:
3434
def backtrack(pos):
3535
"""
3636
Define the recursive backtracking function.
37-
pos is the current position in the
37+
pos is the current position in the
3838
permutation we are filling (1-indexed).
3939
We try to assign a number to position pos.
4040
"""
@@ -47,9 +47,9 @@ def backtrack(pos):
4747
): # Try every candidate number num for the current position pos.
4848
"""
4949
Two checks in one:
50-
1. not used[num] — the number num has
50+
1. not used[num] — the number num has
5151
not been placed yet (we can use it).
52-
2. (num % pos == 0 or pos % num == 0) —
52+
2. (num % pos == 0 or pos % num == 0) —
5353
the beautiful-arrangement condition:
5454
either num divides pos or pos divides num.
5555
If both are true, num is a valid choice for pos.

0 commit comments

Comments
 (0)