[Swift]LeetCode526. 優美的排列 | Beautiful Arrangement

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-zwtsuqap-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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:html

  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?git

Example 1:github

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.

假設有從 1 到 N 的 N 個整數,若是從這 N 個數字中成功構造出一個數組,使得數組的第 i 位 (1 <= i <= N) 知足以下兩個條件中的一個,咱們就稱這個數組爲一個優美的排列。條件:微信

  1. 第 i 位的數字能被 i 整除
  2. i 能被第 i 位上的數字整除

如今給定一個整數 N,請問能夠構造多少個優美的排列?this

示例1:spa

輸入: 2
輸出: 2
解釋: 

第 1 個優美的排列是 [1, 2]:
  第 1 個位置(i=1)上的數字是1,1能被 i(i=1)整除
  第 2 個位置(i=2)上的數字是2,2能被 i(i=2)整除

第 2 個優美的排列是 [2, 1]:
  第 1 個位置(i=1)上的數字是2,2能被 i(i=1)整除
  第 2 個位置(i=2)上的數字是1,i(i=2)能被 1 整除

說明:code

  1. N 是一個正整數,而且不會超過15。

Runtime: 52 ms
Memory Usage: 18.5 MB
 1 class Solution {
 2     func countArrangement(_ N: Int) -> Int {
 3         var nums:[Int] = [Int](repeating:0,count:N)
 4         for i in 0..<N
 5         {
 6             nums[i] = i + 1
 7         }
 8         return helper(N, &nums)
 9     }
10     
11     func helper(_ n:Int,_ nums:inout [Int]) -> Int
12     {
13         if n <= 0 {return 1}
14         var res:Int = 0
15         for i in 0..<n
16         {
17             if n % nums[i] == 0 || nums[i] % n == 0
18             {
19                 (nums[i], nums[n - 1]) = (nums[n - 1], nums[i])
20                 res += helper(n - 1, &nums)
21                 (nums[i], nums[n - 1]) = (nums[n - 1], nums[i])
22             }
23         }
24         return res
25     }
26 }

424mshtm

 1 class Solution {
 2     func countArrangement(_ N: Int) -> Int {
 3         var used = [Bool](repeating: false, count: N + 1)
 4         var result = 0
 5         backtrack(1, N, &used, &result)
 6         return result
 7     }
 8     
 9     private func backtrack(_ position: Int, _ n: Int, _ used: inout [Bool], _ result: inout Int) {
10         if position > n {
11             result += 1
12         } else {
13             for i in 1...n where !used[i] && (position % i == 0 || i % position == 0) {
14                 used[i] = true
15                 backtrack(position + 1, n, &used, &result)
16                 used[i] = false
17             }
18         }
19     }
20 }

452msblog

 1 class Solution {
 2     var count = 0
 3     func countArrangement(_ N: Int) -> Int {
 4         var elements: [Bool] = Array(repeating: false, count: N)
 5         createNext(elements: &elements, position: 1, n: N)
 6         return count;
 7     }
 8     
 9     func createNext(elements: inout [Bool], position: Int, n: Int) {
10         if position > n {
11             count += 1
12             return
13         }
14         for i in 0..<elements.count {
15             if !(!elements[i] && ( position % (i+1) == 0 || (i+1) % position == 0)) { continue; }
16             elements[i] = true;
17             createNext(elements: &elements, position: position+1, n: n)
18             elements[i] = false
19         }
20     }
21 }

536ms

 1 class Solution {
 2     func countArrangement(_ N: Int) -> Int {
 3       guard N > 0 else { return 0 }
 4       
 5       var visited = Array.init(repeating: 0, count: N + 1)
 6       var count = 0
 7       func dfs(position: Int) {
 8         
 9         if position > N {
10           count += 1
11           return
12         }
13         
14         for i in 1..<(N+1) {
15           if visited[i] == 0 && (i % position == 0 || position % i == 0) {
16             visited[i] = 1
17             dfs(position: position + 1)
18             visited[i] = 0
19           }
20         }
21         
22       }
23       
24       dfs(position: 1)
25       return count
26     }
27 }

820ms

 1 class Solution {
 2     func countArrangement(_ N: Int) -> Int {
 3         var num = 0
 4         var mark = Array(repeatElement(0, count: N + 1))
 5         insertNum(N: N, index: 1, mark: &mark, result: &num)
 6         return num
 7     }
 8     
 9     func insertNum(N: Int, index: Int, mark: inout [Int], result: inout Int) {
10         if N == index - 1 {
11             result += 1
12             return
13         }
14         
15         for j in 1...N {
16             if mark[j] == 0 && (j % index == 0 || index % j == 0) {
17                 mark[j] = 1
18                 insertNum(N: N, index: index + 1, mark: &mark, result: &result)
19                 mark[j] = 0
20             }
21         }
22     }
23 }
相關文章
相關標籤/搜索