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.

Approach #1: backtracking. [C++]

class Solution {
public:
    int countArrangement(int N) {
        vector<int> path;

        for (int i = 1; i <= N; ++i)
            path.push_back(i);

        return helper(N, path);
    }

private:
    int helper(int n, vector<int> path) {
        if (n <= 0) return 1;
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            if (path[i] % n == 0 || n % path[i] == 0) {
                swap(path[i], path[n-1]);
                ans += helper(n-1, path);
                swap(path[i], path[n-1]);
            }
        }
        return ans;
    }
};

  

原文地址:https://www.cnblogs.com/ruruozhenhao/p/10354265.html

时间: 2024-08-29 18:52:00

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

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,

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|

【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 

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.