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 ...
随机推荐
- 将DataTable中的数据导出成Excel
public bool ExportFile(System.Data.DataTable dt){ SaveFileDialog sfd = new SaveFileDialog(); s ...
- zoj1037-Gridland
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=37 Gridland Time Limit: 2 Seconds Me ...
- cf499A-Watching a movie
http://codeforces.com/problemset/problem/499/A A. Watching a movie You have decided to watch the b ...
- 解剖Nginx·模块开发篇(4)模块开发中的命名规则和模块加载与运行流程
1 命名规则 1.1 基本变量 基本变量有三个: ngx_module_t 类型的 ngx_http_foo_bar_module: ngx_command_t 类型的数组 ngx_http_foo_ ...
- labelme2COCO
# -*- coding:utf-8 -*-# !/usr/bin/env python import argparseimport jsonimport matplotlib.pyplot as p ...
- hadoop组件启动和关闭命令
一.启动相关组件之前 一般安装完hadoop之后需要格式化一遍hdfs: hdfs namenode -format 然后再进行其他组件的启动,hadoop相关组件都是用位于...hadoop/sbi ...
- SLAM Course - WS13/14 by Cyrill Stachniss (1) 课程资源汇总
本帖是作者学习SLAM 课程笔记的资源帖,汇总了SLAM Course - WS13/14 by Cyrill Stachniss 的相关资源. 1. 课程网站,有相关课件作业和教学视频下载. htt ...
- p2408 不同子串个数
传送门 分析 首先我们不难求出一共有多少子串 之后我们只需要减掉重复个数即可 于是我们对于每个后缀减去它跟它前一名的最长公共前缀即可 代码 #include<iostream> #incl ...
- 使用python进行汉语分词-乾颐堂
目前我常常使用的分词有结巴分词.NLPIR分词等等 最近是在使用结巴分词,稍微做一下推荐,还是蛮好用的. 一.结巴分词简介 利用结巴分词进行中文分词,基本实现原理有三: 基于Trie树结构实现高效的词 ...
- [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 ...