LC 526. Beautiful Arrangement
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:
- 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.
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的更多相关文章
- 526. Beautiful Arrangement
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...
- LC 667. Beautiful Arrangement II
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- 【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 932. Beautiful Array
For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...
随机推荐
- 第十五章、线程之queue模块的各种队列
目录 第十五章.线程之queue模块的各种队列 一.Queue 二.LifoQueue堆栈 三.PriorityQueue优先级队列 第十五章.线程之queue模块的各种队列 一.Queue impo ...
- SDU&PDU
SDU(service Data Unit):服务数据单元,又叫业务数据单元,是指定层的用户服务的数据集,传送到接收方的时候同一协议层时数据没有发生变化,即业务部分,然后发给下层之后,下层将其封装在P ...
- 论文笔记:Unsupervised Domain Adaptation by Backpropagation
14年9月份挂出来的文章,基本思想就是用对抗训练的方法来学习domain invariant的特征表示.方法也很只管,在网络的某一层特征之后接一个判别网络,负责预测特征所属的domain,而后特征提取 ...
- mysql数据库:mysql增删改、单表、多表及子查询
一.数据增删改 二.单表查询 三.正表达式匹配 四.多表查询 五.子查询 一..数据增删改 增加 insert [into] 表名[(可选字段名)] values(一堆值1),( ...
- 下拉菜单 Spinner 简单纯字符串版
下拉菜单 Spinner 简单纯字符串版 public class MainActivity extends Activity implements AdapterView.OnItemSelecte ...
- 51Nod 1534 棋盘阻挡博弈
很简单的可以知道 如果P在V的右上角 必输 如果P在V的左下角 必赢 接下里还剩下左上角和右下角两种情况 两种情况其实相同 P是挡不住V通过对角线方向向下/左的移动的 即两者不会相互影响 所以我们只要 ...
- 部署NFS共享
一:NFS工作原理 什么是NFS服务器 NFS就是Network File System的缩写,它最大的功能就是可以通过网络,让不同的机器.不同的操作系统可以共享彼此的文件. NFS服务器可以让PC将 ...
- 部署jenkins+git
Jenkins简介 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能 安装并启动思路: 安装准备 ...
- nginx基本概述
上级回顾: 1.NFS 2.Sersync 3.SSH 1.ssh使用两种登录服务器的方式,哪两种? 密码 用户名 + 密码 秘钥 用户名 + 秘钥(私钥) 公钥加密 私钥解密 2.大家常说的 塞ke ...
- Js基础知识(四) - js运行原理与机制
js运行机制 本章了解一下js的运行原理,了解了js的运行原理才能写出更优美的代码,提高运行效率,还能解决开发中遇到的不理解的问题. 进程与线程 进程是cpu资源分配的最小单位,进程可以包含多个线程. ...