uppose 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.

Runtime: 168 ms, faster than 18.79% of C++ online submissions for Beautiful Arrangement.

自己的解法就是暴力搜索。

#include <vector>
#include <set>
#include <iostream>
using namespace std;
class Solution {
public:
int countArrangement(int N) {
vector<bool> visited(N,false);
int ret = ;
helper(visited, ret, );
return ret;
}
void helper(vector<bool>& visited, int& ret, int idx){
if(idx == visited.size()){
ret++;
return;
}
for(int i=; i < visited.size(); i++){
if(!visited[i] && ((i+) % (idx+) == || (idx+) % (i+) == )){
visited[i] = true;
helper(visited, ret, idx+);
visited[i] = false;
}
}
}
};

看一个比较巧妙的,把每一个数和最后一个index对比,如果满足条件,就把n-1的情况加到结果中,因为这相当于在n-1的结果中加入了后一项,长度增加1,但是数量还是n-1的数量,所以可以直接加

到结果中,但是这里的helper需要带nums,因为交换以后的nums不是1-n了。

class Solution {
//generate all permutaitons swapping from the back and check if valid
private:
int helper(int n, vector<int>& nums){ //checking index == n
if (n <= ){ //a single num is always valid
return ;
}
int res = ;
for (int i = n-; i >= ; i--){
if (nums[i] % n == || n % nums[i] == ){
swap(nums[i], nums[n-]);
res += helper(n-, nums);
swap(nums[i], nums[n-]);
}
}
return res;
}
public:
int countArrangement(int N) {
vector<int> nums;
for (int i = ; i <= N; i++){
nums.push_back(i);
}
return helper(N, nums);
}
};

LC 526. Beautiful Arrangement的更多相关文章

  1. 526. Beautiful Arrangement

    Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...

  2. LC 667. Beautiful Arrangement II

    Given two integers n and k, you need to construct a list which contains n different positive integer ...

  3. 【LeetCode】526. Beautiful Arrangement 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. [LeetCode] Beautiful Arrangement II 优美排列之二

    Given two integers n and k, you need to construct a list which contains n different positive integer ...

  5. [LeetCode] Beautiful Arrangement 优美排列

    Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...

  6. [Swift]LeetCode526. 优美的排列 | Beautiful Arrangement

    Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...

  7. LeetCode Beautiful Arrangement II

    原题链接在这里:https://leetcode.com/problems/beautiful-arrangement-ii/description/ 题目: Given two integers n ...

  8. LeetCode Beautiful Arrangement

    原题链接在这里:https://leetcode.com/problems/beautiful-arrangement/description/ 题目: Suppose you have N inte ...

  9. LC 932. Beautiful Array

    For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...

随机推荐

  1. C++ 内联函数 摘自 C++ 应用程序性能优化

    内联函数 在C++语言的设计中,内联函数的引入可以说完全是为了性能的考虑.因此在编写对性能要求比较高的C++程序时,非常有必要仔细考量内联函数的使用. 所谓"内联",即将被调用函数 ...

  2. python常用模块:pickle、shelve、json、xml、configparser

    今日内容主要有: 一.pickle模块二.shelve模块三.json模块四.json练习五.xml模块 六.xml练习七.configparser模块 一.pickle模块 #pickle是一个用来 ...

  3. datePicker 及 timePicker 监听事件 获取用户选择 年月日分秒信息

    public class MainActivity extends AppCompatActivity { private TimePicker timePicker; private DatePic ...

  4. dedecms织梦后台发布文章提示“标题不能为空”的解决办法

    V5.7登录后台后,发布英文标题没问题,发布中文会提示“标题不能为空”. 原因:htmlspecialchars在php5.4默认为utf8编码,gbk编码字符串经 htmlspecialchars ...

  5. python 示例代码3

    示例3:Python获取当前环境下默认编码(字符编码demo1.py) 字符编码,python解释器在加载py文件中的代码时,会对内容进行编码(默认ASCII),windows系统默认编码为GBK,U ...

  6. Python——缓冲区

    原创声明:本文系博主原创文章,转载及引用请注明出处. 1. 在Python中,字符串和整型对象都是不可变的(immutable)类型,因此Python会很高效地缓存它们. 2. Python2.3简单 ...

  7. 阿里云-docker安装rabbitmq及无法访问主页

    一.下载最新的rabbitmq docker pull rabbitmq 二.下载完以后启动容器 docker run -d -p 5672:5672 -p 15672:15672 --name my ...

  8. 编程中易犯错误汇总:一个综合案例.md

    # 11编程中易犯错误汇总:一个综合案例 在上一篇文章中,我们学习了如何区分好的代码与坏的代码,如何写好代码.所谓光说不练假把式,在这篇文章中,我们就做一件事——一起来写代码.首先,我会先列出问题,然 ...

  9. 在linux 安装python

    wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz tar -zxvf Python-3.7.1.tgz cd Python-3 ...

  10. 坚果Pro2刷魔趣系统教程,刷回锤子系统教程

    一.刷魔趣系统 1.高通驱动安装 https://blog.csdn.net/qq_43653944/article/details/86702169 2.刷入twrp rec https://blo ...