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. Delphi CreateFile函数

  2. vim 绑定到 source insight 快捷键

    1. optioons -> custom commands 2. 选择然后写入run命令: "D:\Program Files (x86)\Vim\vim74\gvim.exe&qu ...

  3. 《python解释器源码剖析》第9章--python虚拟机框架

    9.0 序 下面我们就来剖析python运行字节码的原理,我们知道python虚拟机是python的核心,在源代码被编译成字节码序列之后,就将有python的虚拟机接手整个工作.python虚拟机会从 ...

  4. -bash: ls: No such file or directory 错误的原因及解决办法

    ubuntu出现如下错误: { Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.15.0-42-generic x86_64) * Documentation: ...

  5. 【转载】GAN for NLP 论文笔记

    本篇随笔为转载,原贴地址,知乎:GAN for NLP(论文笔记及解读).

  6. java8学习之BiFunction函数式接口实例演示&Predicate函数式接口详解

    BiFunction函数式接口: 在上次中已经对BiFunction接口进行了初步的认识,这里对它进一步学习,这里打算新建一个Person实体,然后新建若干个Person的实例存放在集合中,最后再根据 ...

  7. TOMCAT控制台日志输出到指定文件中

    1 .修改startup.bat第42行 call "%EXECUTABLE%" start %CMD_LINE_ARGS% 为 call "%EXECUTABLE%&q ...

  8. hdu1529 Cashier Employment[差分约束+二分答案]

    这题是一个类似于区间选点,但是有一些不等式有三个未知量参与的情况. 依题意,套路性的,将小时数向右平移1个单位后,设$f_i$为前$i$小时工作的人数最少是多少,$f_{24}$即为所求.设$c_i$ ...

  9. python中sys.argv[]用法

    sys.argv[]的作用: 在运行python文件的时候往文件里面传递参数. 从函数外部获取到变量值 import sys arg = sys.argv[0] args = sys.argv[:] ...

  10. Practical, Dynamic Visibility for Games(可实现动态显示技术)

    Practical, Dynamic Visibility for Games(可实现动态显示技术) 原文地址 1引言 游戏场景越来越复杂,包含的内容越来越多,动态显示技术很需要. 本文介绍2种互补的 ...