526. Beautiful Arrangement (Medium)
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position (1 <= i <= N) in this array:
- The number at the ith position is divisible by i.
- i is divisible by the number at the ith position.
Now given N, how many beautiful arrangements can you construct?
Example 1:
Input: 2 Output: 2 Explanation:
The first beautiful arrangement is [1, 2]:
Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1).
Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2).
The second beautiful arrangement is [2, 1]:
Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1).
Number at the 2nd position (i=2) is 1, and i (i=2) is divisible by 1.
Note:
- N is a positive integer and will not exceed 15.
题意:第i个位置的数字可以被i整除,i可以被第i个位置的数字整除;
思路:
1.idx为当前数字的下标,nums为剩余待选数字;
2.初始令index = 1, nums = [1 .. N];
3.遍历nums,记当前数字为n,除n以外的其余元素为nums[:i]+nums[i+1:];
4.若n满足题设的整除条件,则将numSearch(index + 1, nums[:i]+nums[i+1:])累加至ans;
class Solution(): def countArrangement(self, N): """ :type N: int :rtype: int """ temp = {} def numSearch(index, nums): if not nums: return 1 key = index, tuple(nums) if key in temp: return temp[key] ans = 0 for i, n in enumerate(nums): if n % index == 0 or index % n == 0: ans += numSearch(index + 1, nums[:i] + nums[i+1:]) temp[key] = ans return ans return numSearch(1, range(1, N + 1))