成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 526. Beautiful Arrangement

GeekQiaQia / 958人閱讀

Problem

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.

Solution
class Solution {
    int count = 0;
    public int countArrangement(int N) {
        //for each position, fill with 1-N using dfs
        //when position moved to N+1, we found another arrangement
        boolean[] used = new boolean[N+1];
        dfs(1, N, used);
        return count;
    }
    private void dfs(int index, int len, boolean[] used) {
        if (index > len) {
            count++;
            return;
        }
        for (int i = 1; i <= len; i++) {
            if (!used[i] && (i%index == 0 || index%i == 0)) {
                used[i] = true;
                dfs(index+1, len, used);
                used[i] = false;
            }
        }
    }
}

文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉載請注明本文地址:http://systransis.cn/yun/72234.html

相關文章

  • 526. Beautiful Arrangement and 254. Factor Combina

    摘要:用的思想,加上題目的特殊意思,來解決問題。如果個數(shù)字,有種結果。這里對原題多加了一個輸出的要求,代碼如下。也是為了去重,兩個分解的解法是對稱的,乘法對稱的重點就是 用combination的思想,加上題目的特殊意思,來解決問題。 526 Beautiful Arrangement Suppose you have N integers from 1 to N. We define a ...

    I_Am 評論0 收藏0
  • Leetcode 相似題只有題號不含代碼。

    找出string里的單詞。 186. Reverse Words in a String II, 434. Number of Segments in a String combination類型題 77. Combinations 39. Combination Sum 40. Combination Sum II 216. Combination Sum III 494. Target S...

    StonePanda 評論0 收藏0
  • leetcode31 Next Permutation

    摘要:如果當前數(shù)字代表的整數(shù)值已經(jīng)是所有排列組合中的最大值,則返回當前數(shù)字組成的最小值。可是這意味著大量無用的數(shù)字的生成和比較。一個數(shù)字中的各個位上的數(shù)如何調(diào)整順序才能獲得一個最小的更大值。其次,要保證移動之后,高位以后的值為最小值。 題目要求 Implement next permutation, which rearranges numbers into the lexicographi...

    hedzr 評論0 收藏0
  • leetcode 31 Next Permutation

    摘要:我們所找到的這個元素就是排序需要改變的第一個元素。然后我們選取一個剛好大于此元素的數(shù),與當前元素進行替換。并對后面的所有元素重新按照升序排列就可以得到最終的答案。 題目詳情 Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of...

    binaryTree 評論0 收藏0
  • [Leetcode] Next Permutation 下一個排列

    摘要:因為增加高位會帶來更大的增益。所以對于一個長為的序列,我們增加第位的前提是,前位已經(jīng)達到了最大排列方法。因為是找下一個數(shù),所以我們要找一個比小卻盡可能大的數(shù),所以找到。把換到的位置后,后三位仍然是個降序的排列。 Next Permutation Implement next permutation, which rearranges numbers into the lexicogr...

    young.li 評論0 收藏0

發(fā)表評論

0條評論

GeekQiaQia

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<