526. Beautiful Arrangement 美丽排列

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:

  1. The number at the ith position is divisible by i.
  2. 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:

  1. N is a positive integer and will not exceed 15.

如果整数1到N的排列,第i个数满足下列规则之一,则称该排列为“美丽排列”

  1. 第i个位置的数字可以被i整除
  2. i可以被第i个位置的数字整除

给定数字N,求有多少个美丽排列

  1. class Solution(object):
  2. def countArrangement(self, N):
  3. if N == 0:
  4. return 0
  5. nums = [0 for x in range(0,N+1)]
  6. return self.helper(N,1,nums)
  7. def helper(self,N,pos,used):
  8. if pos > N:
  9. return 1
  10. num = 0
  11. for i in range(1,N+1):
  12. if used[i] == 0 and (i%pos==0 or pos%i==0):
  13. used[i] = 1
  14. num += self.helper(N,pos+1,used)
  15. used[i] = 0
  16. return num
  1. class Solution(object):
  2. def countArrangement(self, N):
  3. """
  4. :type N: int
  5. :rtype: int
  6. """
  7. return [0, 1, 2, 3, 8, 10, 36, 41, 132, 250, 700, 750, 4010, 4237, 10680, 24679][N]

来自为知笔记(Wiz)

时间: 2024-10-05 02:44:07

526. Beautiful Arrangement 美丽排列的相关文章

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 positi

526. Beautiful Arrangement

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 positi

667. Beautiful Arrangement II-- 类比526 但不能用back tracking

667 是很坑爹的一个题目,乍一看和 526 如出一辙, 526. Beautiful Arrangement 题意: 构造 [1,n]的排列,让每个 a[index] % index ==0 或者 index %a[index] ==0,  基本和 46 题一样,就是构造排列. 667题意: 给定两个整数n和k,构建一个n个元素数组,数组要满足一下要求: 假设数组[a1,a2....an]那么[|a2-a1|,|a3-a2|.....]包含k个不同的整数.如果有多个数组输出任意一个数组.1<=

Beautiful Arrangement

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

[LeetCode] Beautiful Arrangement

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 positi

HDU 5321 Beautiful Set 美丽集合

题意:给定一个集合,含有n个数.浙理工先生和杭电先生各自有计算这个集合美丽值的方法. 浙理工先生的计算方法是:对于这个n个数的某个排列,此排列的美丽值为这个排列所有的区间最大公约数之和.然后这个集合的美丽值为n个数的所有排列的美丽值之和. 杭电先生的计算方法是:在这个n个数中选出k(1 ≤ k ≤ n)个数,对于某种选取方案,这种方案的美丽值为k个数的最大公约数乘上k.然后这个集合的美丽值为所有选数方案的美丽值之和. 然后他们想比比谁得到的美丽值更大.因为数很大,所以他们只比较各自的结果对258

Beautiful Arrangement II

这是一个中等题 题目: 思路: 我是创建一个新列表,列表最开始按一个最低,一个最高排列,如果p==k-1了,那么就停止,把剩下元素依次排序,比如n=10,k=3,我就先排1,然后把10加进去,这时p=1,再把2加进去,这时p=2,满足了p=k-1,然后把剩下元素按2,3,4,5,6,7,8,9排序加入列表中.之所以这么做是因为他的差值不会重复,1和10的差最大了,2和10的次大....... 代码: class Solution(object): def constructArray(self,

【leetcode】667. Beautiful Arrangement II

题目如下: Given two integers n and k, you need to construct a list which contains ndifferent positive integers ranging from 1 to n and obeys the following requirement: Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 

667. Beautiful Arrangement II

Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n and obeys the following requirement: Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|