-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path526-beautiful-arrangement.py
73 lines (57 loc) · 2.03 KB
/
526-beautiful-arrangement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Problem Link: https://leetcode.com/problems/beautiful-arrangement/
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.
Example 1:
Input: n = 2
Output: 2
Explanation:
The first beautiful arrangement is [1,2]:
- perm[1] = 1 is divisible by i = 1
- perm[2] = 2 is divisible by i = 2
The second beautiful arrangement is [2,1]:
- perm[1] = 2 is divisible by i = 1
- i = 2 is divisible by perm[2] = 1
Example 2:
Input: n = 1
Output: 1
Constraints:
1 <= n <= 15
"""
class Solution:
def countArrangement(self, n: int) -> int:
nums = list(range(1, n+1))
self.count = 0
self.permute(nums, 0)
return self.count
def permute(self, nums, pos):
if pos == len(nums):
self.count += 1
for i in range(pos, len(nums)):
nums[i], nums[pos] = nums[pos], nums[i]
if (nums[pos] % (pos + 1) == 0) or ((pos + 1) % nums[pos] == 0):
self.permute(nums, pos+1)
nums[i], nums[pos] = nums[pos], nums[i]
# Time Complexity -> O(n!)
# TLE
class Solution1:
def countArrangement(self, n: int) -> int:
nums = list(range(1, n+1))
self.count = 0
self.permute(nums, 0)
return self.count
def permute(self, nums, pos):
if pos == len(nums) - 1 and self.isValid(nums):
self.count += 1
for i in range(pos, len(nums)):
nums[i], nums[pos] = nums[pos], nums[i]
self.permute(nums, pos+1)
nums[i], nums[pos] = nums[pos], nums[i]
def isValid(self, nums):
for k in range(1, len(nums)+1):
if nums[k-1] % k != 0 and k % nums[k-1] != 0:
return False
return True