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. Linux编译安装GCC

    1. 下载gcc安装包 网址:http://ftp.gnu.org/gnu/gcc/ ,下载对应的安装包,我选择gcc-5.5.0.tar.gz 2. 下载依赖库 一个是mpc,一个是gmp,一个是m ...

  2. python常用模块:项目目录规范、定制程序入口、自定义模块、引用变量、time和datetime模块

    今天讲课内容有两大部分: 一.文件目录规范二.定制程序入口三.使用标准目录后四.常规函数time.datetime 一.标准目录规范 之前用过的案例atm机+购物商城代码过长,在学习了模块和包以后,我 ...

  3. Linux使用wget仿站

    运行命令 $ wget -r -p -np -k www.avatrade.cn 参数说明 -r --recursive(递归) specify recursive download.(指定递归下载) ...

  4. linux基础—课堂随笔09_数组

    数组:(6.14 第一节) 取分区利用率,大于百分之八十则发出警报 取分区第一列 取分区使用率: 脚本: 检查脚本语法: ——end 数组切片: 1.跳过前两个取后面 2.跳过前两个取三个 生成10个 ...

  5. centos7安装BitCoin客户端

    一.安装依赖环境 [root@localhost src]# yum install autoconf automake libtool libdb-devel boost-devel libeven ...

  6. POJ 1741 单次询问树上距离<=K的点对数 点分治

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; ; ], ...

  7. 【SDR】UHD安装教程

    USRP作为软件无线电系统中常用的射频设备,其驱动UHD的安装及稳定运行,是SDR系统稳定的必备条件,该篇博客总结UHD的相关安装方法,主要有三种,分别是apt-get.github clone源码编 ...

  8. 集合(七) Set—HashSet,TreeSet和LinkedHashSet

    四.Set Set和List一样,也是继承Collection的接口,但Set是不包含重复元素的集合.由于先啃下Map,Set的难度将会大幅减小.因为Set基本上都是以Map为基础实现的,例如两个主要 ...

  9. Centos7网卡配置命令nmcli

    在配置Centos6时,大家第一想到的就是把networkManager这个服务关掉,让它消失,这个服务台太鸡肋了,不该起作用的时候经常起作用,给管理带来了不便,但是在Centos7当中network ...

  10. python OS模块 对操作系统接口调用

    os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cdos.curdir ...