- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 49.1k
Create beautiful-arrangement #13779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create beautiful-arrangement #13779
Changes from 11 commits
4fcf7ff
              3a5a18c
              1448767
              fb5ecf4
              69a579b
              342669a
              8e9407b
              e2c218b
              cd38be7
              adf05c5
              4965e59
              d96f565
              5e0a91d
              c2c10ee
              88d1ecf
              ca34f0a
              898b97f
              cf8d7bb
              61d5edd
              eff3196
              dab1c52
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| """ | ||
| Suppose you have n integers labeled 1 through n. | ||
| A permutation of those n integers | ||
| perm (1-indexed) is considered a | ||
| "beautiful arrangement" if for every i (1 <= i <= n), | ||
| either of the following is true: | ||
|  | ||
| -> perm[i] is divisible by i. | ||
| -> i is divisible by perm[i]. | ||
| Given an integer n, return the number of the | ||
| "beautiful arrangements" that you can construct. | ||
|  | ||
| """ | ||
| # Solution using Backtracking | ||
|  | ||
|  | ||
| class BeautifulArrange: | ||
| # funtion call; n is the size of the permutation (numbers 1..n) | ||
| def countarrangement(self, n: int) -> int: | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file  Please provide descriptive name for the parameter:  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file  Please provide descriptive name for the parameter:  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file  Please provide descriptive name for the parameter:  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file  Please provide descriptive name for the parameter:  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file  Please provide descriptive name for the parameter:  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file  Please provide descriptive name for the parameter:  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file  Please provide descriptive name for the parameter:  | ||
| self.count = 0 | ||
| """ | ||
| We initialize a counter to record how | ||
| many valid arrangements we find. | ||
| Using self.count lets the nested | ||
| function modify it without nonlocal. | ||
| """ | ||
|  | ||
| used = [False] * (n + 1) | ||
| """ | ||
| A boolean list to mark which numbers have | ||
| already been placed in the permutation. | ||
| """ | ||
|  | ||
| def backtrack(pos): | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file  Please provide return type hint for the function:  Please provide type hint for the parameter:  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file  Please provide return type hint for the function:  Please provide type hint for the parameter:  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file  Please provide return type hint for the function:  Please provide type hint for the parameter:  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file  Please provide return type hint for the function:  Please provide type hint for the parameter:  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file  Please provide return type hint for the function:  Please provide type hint for the parameter:  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file  Please provide return type hint for the function:  Please provide type hint for the parameter:  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function:  As there is no test file in this pull request nor any test function or class in the file  Please provide type hint for the parameter:  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function:  As there is no test file in this pull request nor any test function or class in the file  Please provide type hint for the parameter:  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function:  As there is no test file in this pull request nor any test function or class in the file  Please provide type hint for the parameter:  | ||
| """ | ||
| Define the recursive backtracking function. | ||
| pos is the current position in the | ||
| permutation we are filling (1-indexed). | ||
| We try to assign a number to position pos. | ||
| """ | ||
| if pos > n: | ||
| self.count += 1 | ||
| # We found a complete valid arrangement, so increment the total count. | ||
| return | ||
| for num in range( | ||
| 1, n + 1 | ||
| ): # Try every candidate number num for the current position pos. | ||
| """ | ||
| Two checks in one: | ||
| 1. not used[num] — the number num has | ||
| not been placed yet (we can use it). | ||
| 2. (num % pos == 0 or pos % num == 0) — | ||
| the beautiful-arrangement condition: | ||
| either num divides pos or pos divides num. | ||
| If both are true, num is a valid choice for pos. | ||
|  | ||
| """ | ||
| if not used[num] and (num % pos == 0 or pos % num == 0): | ||
| used[num] = True | ||
| backtrack(pos + 1) | ||
| used[num] = False | ||
|  | ||
| backtrack(1) | ||
| return self.count | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
dynamic_programming/beautiful_arrangement.py, please provide doctest for the functioncountarrangementPlease provide descriptive name for the parameter:
n