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;
}
};

  

526. Beautiful Arrangement的更多相关文章

  1. LC 526. Beautiful Arrangement

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

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

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

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

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

  4. [LeetCode] Beautiful Arrangement 优美排列

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

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

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

  6. LeetCode Beautiful Arrangement II

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

  7. LeetCode Beautiful Arrangement

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

  8. LC 667. Beautiful Arrangement II

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

  9. [Swift]LeetCode667. 优美的排列 II | Beautiful Arrangement II

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

随机推荐

  1. Request.IsAuthenticated

    Original question that the answer below refers to: I have a forms based application that is giving m ...

  2. python's fnmatch&glob&os.listdir

    [python's fnmatch&glob&os.listdir] fnmatch: fnmatch只有4种special character,用于提供和shell中一样的文件名的匹 ...

  3. http和socket之长连接和短连接区别(转)

    TCP/IP TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层. 在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议. 在传输层中有TCP协议与UDP协议. 在应 ...

  4. distributed lock manager (DLM)(分布式管理锁)

    A distributed lock manager (DLM) provides distributed software applications with a means to synchron ...

  5. Cocos2dx之touch事件

    今天看了下ccocos2dx touch事件部分的源码,从CCTouch.CCTouchHandler和CCTouchDispatcher简单的做了分析和总结,先直接看源码吧! 1.CCTouch c ...

  6. [C++] decltype(auto) C++ 11 feature

    1 //C++ 11 feature template <class T1, class T2> auto getMultiply(T1 data1, T2 data2) -> de ...

  7. STM32F4通用定时器

    1.基本原理 三种定时器区别 通用定时器功能特点描述 在这里只用输入捕获事件也能获取脉冲个数同时可以只使用它来获取脉冲宽度,比如当捕获到上升沿,马上进入中断,把计数器的值置零,然后等待捕获下降沿的到来 ...

  8. nignx ssl 配置

    1. 找一个目录,例如: usr/local/nginx/ssl ,进入该目录 2.  openssl genrsa -des3 -out server.key 1024  创建自身秘钥 3.  op ...

  9. git reset --soft --hard 区别

    [转]git reset 之 soft mixed hard选项的区别 (2014-09-09 16:54:06) 转载▼ 标签: git 分类: Linux 译注:为了避免丢失本地的修改以及orig ...

  10. smarty中用truncate来截取中英文字符串及避免中文乱码问题

    smarty中用truncate来截取含有中英文的字符串,可能会出现中文乱码问题.字符串截取长度不一问题,下面是新建个扩展函数,或修改原Truncate函数方法也可以的.扩展smarty/plugin ...