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 position is divisible by i.
- 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:
- 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的更多相关文章
- LC 526. Beautiful Arrangement
uppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constr ...
- 【LeetCode】526. Beautiful Arrangement 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Beautiful Arrangement II 优美排列之二
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- [LeetCode] Beautiful Arrangement 优美排列
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...
- [Swift]LeetCode526. 优美的排列 | Beautiful Arrangement
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...
- LeetCode Beautiful Arrangement II
原题链接在这里:https://leetcode.com/problems/beautiful-arrangement-ii/description/ 题目: Given two integers n ...
- LeetCode Beautiful Arrangement
原题链接在这里:https://leetcode.com/problems/beautiful-arrangement/description/ 题目: Suppose you have N inte ...
- LC 667. Beautiful Arrangement II
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- [Swift]LeetCode667. 优美的排列 II | Beautiful Arrangement II
Given two integers n and k, you need to construct a list which contains n different positive integer ...
随机推荐
- jar 运行报错:找不到或无法加载主类
NIFEST.MF文件中指定的,如下所示:Manifest-Version: 1.0Class-Path: .Main-Class: com.webex.app.Main // ...
- [iOS]隐藏导航栏把右滑退出操作保留
项目因为用到上面导航栏样式多变,就隐藏了导航栏自己用View代替了,但手势却不见了,后来发现问题解决.操作如下: 千万不要取消 Shows Navigation Bar 这个选项否则手势会消失 应该是 ...
- MySQL数据库篇之pymysql模块的使用
主要内容: 一.pymysql模块的使用 二.pymysq模块增删改查 1️⃣ pymsql模块的使用 1.前言:之前我们都是通过MySQL自带的命令行客户端工具mysql来操作数据库, 那如何在p ...
- delete属性
- 详解C++右值引用
C++0x标准出来很长时间了,引入了很多牛逼的特性[1].其中一个便是右值引用,Thomas Becker的文章[2]很全面的介绍了这个特性,读后有如醍醐灌顶,翻译在此以便深入理解. 目录 概述 mo ...
- zookeeper 阿里滴滴 有点用 zookeeper主从选举方式
面试也经常问kafka的原理,以及zookeeper与kafka原理的区别:kafka 数据一致性-leader,follower机制与zookeeper的区别: zookeeper是如何实现负载均衡 ...
- Ha-Federation-hdfs +Yarn集群部署方式
经过一下午的尝试,终于把这个集群的搭建好了,搭完感觉也没有太大的必要,就当是学习了吧,为之后搭建真实环境做基础. 以下搭建的是一个Ha-Federation-hdfs+Yarn的集群部署. 首先讲一下 ...
- hdu 2049 不容易系列之(4)——考新郎 (错排递推)
当n个编号元素放在n个编号位置,元素编号与位置编号各不对应的方法数用M(n)表示,那么M(n-1)就表示n-1个编号元素放在n-1个编号位置,各不对应的方法数,其它类推. 第一步,把第n个元素放在一个 ...
- 实践作业4:Web测试----细化分工DAY1.
会议时间:2017年12月23日 会议地点:东九教学楼教师休息室 主持人:吴辉 参会人员:吴辉.刘思佳.郜昌磊.王俊杰.吴慧杰 记录人:刘思佳 会议议题:本次作业的分工以及初期安排 工具选择 软件测试 ...
- UVa 3211 Now or later (二分+2-Sat)
题意:有 n 架飞机,每个飞机早着陆,或者晚着陆,让你安排一个方式,让他们着陆的时间间隔尽量大. 析:首先对于时间间隔,可以用二分来解决,然后就成了一个判定性问题,然后怎么判断该时间间隔是不是成立呢, ...