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:

  1. Input: 2
  2. Output: 2
  3. Explanation:
  4.  
  5. The first beautiful arrangement is [1, 2]:
  6.  
  7. Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1).
  8.  
  9. Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2).
  10.  
  11. The second beautiful arrangement is [2, 1]:
  12.  
  13. Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1).
  14.  
  15. 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++]

  1. class Solution {
  2. public:
  3. int countArrangement(int N) {
  4. vector<int> path;
  5.  
  6. for (int i = 1; i <= N; ++i)
  7. path.push_back(i);
  8.  
  9. return helper(N, path);
  10. }
  11.  
  12. private:
  13. int helper(int n, vector<int> path) {
  14. if (n <= 0) return 1;
  15. int ans = 0;
  16. for (int i = 0; i < n; ++i) {
  17. if (path[i] % n == 0 || n % path[i] == 0) {
  18. swap(path[i], path[n-1]);
  19. ans += helper(n-1, path);
  20. swap(path[i], path[n-1]);
  21. }
  22. }
  23. return ans;
  24. }
  25. };

  

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. 将DataTable中的数据导出成Excel

    public bool ExportFile(System.Data.DataTable dt){    SaveFileDialog sfd = new SaveFileDialog();    s ...

  2. zoj1037-Gridland

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=37 Gridland Time Limit: 2 Seconds      Me ...

  3. cf499A-Watching a movie

    http://codeforces.com/problemset/problem/499/A A. Watching a movie   You have decided to watch the b ...

  4. 解剖Nginx·模块开发篇(4)模块开发中的命名规则和模块加载与运行流程

    1 命名规则 1.1 基本变量 基本变量有三个: ngx_module_t 类型的 ngx_http_foo_bar_module: ngx_command_t 类型的数组 ngx_http_foo_ ...

  5. labelme2COCO

    # -*- coding:utf-8 -*-# !/usr/bin/env python import argparseimport jsonimport matplotlib.pyplot as p ...

  6. hadoop组件启动和关闭命令

    一.启动相关组件之前 一般安装完hadoop之后需要格式化一遍hdfs: hdfs namenode -format 然后再进行其他组件的启动,hadoop相关组件都是用位于...hadoop/sbi ...

  7. SLAM Course - WS13/14 by Cyrill Stachniss (1) 课程资源汇总

    本帖是作者学习SLAM 课程笔记的资源帖,汇总了SLAM Course - WS13/14 by Cyrill Stachniss 的相关资源. 1. 课程网站,有相关课件作业和教学视频下载. htt ...

  8. p2408 不同子串个数

    传送门 分析 首先我们不难求出一共有多少子串 之后我们只需要减掉重复个数即可 于是我们对于每个后缀减去它跟它前一名的最长公共前缀即可 代码 #include<iostream> #incl ...

  9. 使用python进行汉语分词-乾颐堂

    目前我常常使用的分词有结巴分词.NLPIR分词等等 最近是在使用结巴分词,稍微做一下推荐,还是蛮好用的. 一.结巴分词简介 利用结巴分词进行中文分词,基本实现原理有三: 基于Trie树结构实现高效的词 ...

  10. [Training Video - 6] [File Reading] [Java] Read Excel File Using Apache POI API

    读取以下两种格式的Excel : *.xls  and *.xlsx 用Apache POI API来实现,需要用到 HSSF 和 XSSF 的类库 HSSF is the POI Project's ...