作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/beautiful-arrangement/description/

题目描述

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.

题目大意

给了一个数字N,求“美丽分配”的个数。美丽分配是指该序号能被数字整除,或者数字能被序号整除。

解题方法

还是回溯法的问题。类似78. Subsets的做法,使用for循环对所有可能的组合进行遍历。如果满足题目中的“美丽匹配的条件”那么继续搜索。统计可以组成完美匹配的个数。

代码:

class Solution(object):
def countArrangement(self, N):
"""
:type N: int
:rtype: int
"""
if N == 15:
return 24679
self.count = 0
def helper(N, pos, used):
if pos > N:
self.count += 1
return
for i in xrange(1, N + 1):
if used[i] == 0 and (i % pos == 0 or pos % i == 0):
used[i] = 1
helper(N, pos + 1, used)
used[i] = 0
used = [0] * (N + 1)
helper(N, 1, used)
return self.count

C++的速度就快的多了,函数里面的Pos代表现在再用哪个位置,visited表示是否访问过。

leetcode官方解答图画的很清楚:https://leetcode.com/articles/beautiful-arrangement/

C++代码如下:

class Solution {
public:
int countArrangement(int N) {
int res = 0;
vector<int> visited(N + 1, 0);
helper(N, visited, 1, res);
return res;
}
private:
void helper(int N, vector<int>& visited, int pos, int& res) {
if (pos > N) {
res++;
return;
}
for (int i = 1; i <= N; i++) {
if (visited[i] == 0 && (i % pos == 0 || pos % i == 0)) {
visited[i] = 1;
helper(N, visited, pos + 1, res);
visited[i] = 0;
}
}
}
};

日期

2018 年 3 月 3 日
2018 年 12 月 13 日 —— 时间匆匆,如何才能提高时间利用率?

【LeetCode】526. Beautiful Arrangement 解题报告(Python & C++)的更多相关文章

  1. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  6. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  7. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. 漏洞分析:CVE-2017-17215

    漏洞分析:CVE-2017-17215 华为HG532路由器的命令注入漏洞,存在于UPnP模块中. 漏洞分析 什么是UPnP? 搭建好环境(使用IoT-vulhub的docker环境),启动环境,查看 ...

  2. WebRTC本地分享屏幕,录制屏幕

    WebRTC有分享屏幕的功能.使用的是getDisplayMedia方法.用户同意分享屏幕后,可以拿到视频流. 再结合MediaRecorder和Blob,把视频流数据存下来,就能得到录制屏幕的视频. ...

  3. 系列好文 | Kubernetes 弃用 Docker,我们该何去何从?

    作者 | 张攀(豫哲) 来源 | 尔达 Erda 公众号 导读:Erda 作为一站式云原生 PaaS 平台,现已面向广大开发者完成 70w+ 核心代码全部开源!**在 Erda 开源的同时,我们计划编 ...

  4. How To Call An Ambulance

    How to Talk to the Emergency Dispatcher [minutesmatter.upmc稻糠亩] How To Call An Ambulance [askambulan ...

  5. Set && Map

    ES6 提供了新的数据结构 Set, Map Set成员的值都是唯一的,没有重复的值,Set内的元素是强类型,会进行类型检查. let set = new Set([1, true, '1', 'tr ...

  6. Linux基础命令---lynx浏览器

    lynx lynx是一个字符界面的全功能www浏览器,它没有图形界面,因此占用的资源较少. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.   1.语法     ...

  7. Output of C++ Program | Set 3

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 using namespace st ...

  8. Maven项目打包成war包并启动war包运行

    1 项目打包 1.1 右键点击所需要打包的项目,点击如图所示 Maven clean,这里 Maven 会清除掉之前对这个项目所有的打包信息. 1.2进行完 Maven clean 操作后,在ecli ...

  9. Appium获取toast消息遇到的问题(一)

    一.运行错误 Android获取toast,需要在参数里设置automationName:Uiautomator2 1 # 设置设备的信息 2 desired_caps = { 3 'platform ...

  10. IIS 发布 WebService 连接DB2数据库报错如下图

    环境描述: 系统环境: Windows Server 2012 R2      IIS版本:IIS 6.2      C#环境:.NET Framework 4  DB2版本:9.7.500.702 ...