[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:

  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整除,或者索引值+1可以被该元素本身整除,即是一个优美数列

给定一个数字N,该数组由1~N组成。要求判断该数组的全排列中有多少个优美数组。

暴力算法思路:

1、求解全排列。

2、对全排列中每个数组进行判断,并统计。

但是该思路的代码会超时。但是十分清晰。

class Solution {
public:
    int countArrangement(int N) {
        int ans = 0;
        vector<vector<int>> res;
        vector<int> nums;
        for (int i = 1; i <= N; i++)
            nums.push_back(i);
        permute(res, nums, 0);
        for (int i = 0; i < res.size(); i++) {
            int cnt = 0;
            for (int j = 1; j <= N; j++) {
                if ((res[i][j - 1] % j == 0) || (j % res[i][j - 1] == 0))
                    cnt++;
            }
            if (cnt == N)
                ans++;
        }
        return ans;
    }

    void permute(vector<vector<int>>& res, vector<int>& nums, int idx) {
        if (idx >= nums.size()) {
            res.push_back(nums);
            return;
        }
        else {
            for (int i = idx; i < nums.size(); i++) {
                swap(nums[i], nums[idx]);
                permute(res, nums, idx + 1);
                swap(nums[idx], nums[i]);
            }
        }
    }
};
// TLE

TLE

回溯法

class Solution {
public:
    int countArrangement(int N) {
        vector<int> nums;
        for (int i = 1; i <= N; i++)
            nums.push_back(i);
        return helper(N, nums);
    }

    int helper(int n, vector<int>& nums) {
        if (n <= 0)
            return 1;
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            if (n % nums[i] == 0 || nums[i] % n == 0) {
                swap(nums[i], nums[n - 1]);
                cnt += helper(n - 1, nums);
                swap(nums[i], nums[n - 1]);
            }
        }
        return cnt;
    }
};
// 8 ms

原文地址:https://www.cnblogs.com/immjc/p/8343663.html

时间: 2024-08-29 18:26:05

[LeetCode] Beautiful Arrangement的相关文章

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

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<=

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

【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 

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 Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea