Leetcode-beautiful arrangement

Source: Internet
Author: User
Tags integer division

Assume there are n integers from 1 to n. If an array is successfully constructed from the N numbers, the I-bit (1 <= I <= N) of the array is) if one of the following conditions is met, we call this array a beautiful arrangement. Condition:

    1. The I-th digit can be divisible by I.
    2. I can be divisible by numbers in the I-th Digit

Given an integer N, how many beautiful Arrays can be constructed?

Example 1:

 
Input: 2 Output: 2 Explanation: 1st beautiful arrays are [1, 2]: 1st positions (I = 1) the number on is 2nd can be divided by I (I = 1), and the number on the position (I = 2) is can be I (I = 2) the 2nd beautiful permutation is [2, 1]: The number on the 1st positions (I = 1) is 2, 2 can be I (I = 1) the number on the Integer Division 2nd position (I = 2) is 1, And I (I = 2) can be divisible by 1

Note:

    1. N is a positive integer and cannot exceed 15.

Reference blog: beautiful arrangement

Method 1:

Train of Thought: Transform the full arrangement. The void DFS function is changed to the int DFS function.

The pseudo code of the int DFS () function is as follows:

 
IntDFS (IntLen ){If(LEN =A certain number ){Return 1;}IntRes =0; Loop process ------ {res+ = DFS (LEN +1,);}ReturnRes ;}

 

In addition, because it is filtered, the judgment conditions are added during the exchange process. The Code is as follows:

Java

 Class  Solution { Public   Int Countarrangement ( Int  N ){  Int [] Nums = New   Int  [N];  For ( Int I = 0; I <n; I ++ ) {Nums [I] = I + 1 ;}  Return DFS (Nums, 0 );} Public   Void Swap ( Int [] Nums, Int I, Int  J ){  Int Temp = Nums [I]; Nums [I] = Nums [J]; Nums [J] = Temp ;}  //  DFS returns the int type.      Public   Int DFS (Int [] Nums, Int  Len ){  If (LEN = Nums. Length ){  Return 1; //  As long as the backtracing termination condition can be performed, that is, the matching arrangement is displayed. The result is + 1.  }  Int Res = 0 ;  For ( Int I = Len; I <nums. length; I ++ ){ //  Nums [I] num [Len] is exchanged. As Len may be 0, + 1. Here Nums [Len] % (I + 1) = 0, instead of true!              If (Nums [Len] % (LEN + 1) = 0 | (LEN + 1) % Nums [I] = 0 ) {Swap (Nums, I, Len); Res + = DFS (Nums, Len + 1 ); Swap (Nums, I, Len );}}  Return  Res ;}} 

C ++:

 Class  Solution {  Public  :  Int DFS (vector <Int > & Nums, Int  Len ){  If (LEN = Nums. Size ()){  Return 1 ;}  Int Res = 0 ;  For ( Int I = Len; I <nums. Size (); I ++ ){  If (Nums [I] % (LEN + 1) = 0 | (LEN + 1) % Nums [I] = 0) {Swap (Nums [I], Nums [Len]); Res + = DFS (Nums, Len + 1 ); Swap (Nums [I], Nums [Len]);}  Return  Res ;}  Int Countarrangement ( Int  N) {Vector < Int > Nums (N );  For ( Int I = 0; I <n; I ++) Nums [I] = I + 1 ; Return DFS (Nums, 0 );}}; 

Method 2:

 Class  Solution {  Public  :  //  Idea: Judge each digit from numbers 1-N, such as 1, 2, 3, 4, and 5.  //  The first digit can be placed: 1 2 3 4 5. The second digit can be placed: 1 2 4 3 digits can only be placed: 3 4 digits can be placed 1 2 4  //  At the same time, numbers that have been placed cannot be used any more. vis [I] = 1 is used for representation.  //  Condition: If (vis [I] = 0 & (I % Len = 0 | Len % I = 0 ))       Int Countarrangement ( Int  N ){  Int Count = 0  ; Vector < Int > Vis (n + 1 , 0  ); DFS (count, N, VIS,  1  );  Return  Count ;}  Void DFS (Int & Count, Int N, vector < Int > & Vis, Int  Len ){  If (LEN> N) {count ++ ;  Return  ;}  For ( Int I = 1 ; I <= N; I ++){  If (Vis [I] = 0 & (I % Len = 0 | Len % I = 0  ) {Vis [I] = 1  ; DFS (count, N, VIS, Len + 1  ); Vis [I] = 0  ;}}}}; 

 

Leetcode-beautiful arrangement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.