634. Find the Derangement of An Array

In combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no element appears in its original position.

There‘s originally an array consisting of n integers from 1 to n in ascending order, you need to find the number of derangement it can generate.

Also, since the answer may be very large, you should return the output mod 109 + 7.

Example 1:

Input: 3
Output: 2
Explanation: The original array is [1,2,3]. The two derangements are [2,3,1] and [3,1,2].
class Solution {
public:
    int findDerangement(int n) {
        /*
        For ith element, we have switch it with one of the previous numbers 1,2,...,i-1, and for each picked number j, for the positions left except the one take by i, j can take anyone of them. So there are dp[i - 2] permutation if j can take the original position of i, and dp[i - 1] permutations if j can not take the original position of i.
*/
        if(n <= 1) return 0;
        vector<long> dp(n+1);
        long mod = 1000000007;
        dp[2] = 1;
        for(int i = 3; i < dp.size(); i++){
            dp[i] = (i - 1) * (dp[i - 1] + dp[i - 2]) % mod;
        }
        return dp[dp.size() - 1];
    }
};
时间: 2024-10-17 22:02:27

634. Find the Derangement of An Array的相关文章

[leetcode-634-Find the Derangement of An Array]

In combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no element appears in its original position. There's originally an array consisting of n integers from 1 to n in ascending order, you need to find the nu

[LeetCode] Find the Derangement of An Array 找数组的错排

In combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no element appears in its original position. There's originally an array consisting of n integers from 1 to n in ascending order, you need to find the nu

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,

JavaScript Array --&gt;map()、filter()、reduce()、forEach()函数的使用

题目: 1.得到 3000 到 3500 之内工资的人. 2.增加一个年龄的字段,并且计算其年龄. 3.打印出每个人的所在城市 4.计算所有人的工资的总和. 测试数据: function getData() { var arr = [{ id: 1, name: 'ohzri', birth: '1999.09.09', city: '湖北', salary: 9379 }, { id: 2, name: 'rqgfd', birth: '1999.10.28', city: '湖北', sal

PHP Warning: array_multisort(): Array sizes are inconsistent

array_multisort() 函数返回排序数组.您可以输入一个或多个数组.函数先对第一个数组进行排序,接着是其他数组,如果两个或多个值相同,它将对下一个数组进行排序. 遇到这报错是两个数组对比不一致导致的, 如果是一维数组与二维数组进行排序可以用以下方法解决: 使用这个方法,会比较麻烦些,要将age提取出来存储到一维数组里,然后按照age升序排列.具体代码如下: 复制代码代码如下: $ages = array();foreach ($users as $user) {    $ages[]

详解go语言的array和slice 【二】

上一篇  详解go语言的array和slice [一]已经讲解过,array和slice的一些基本用法,使用array和slice时需要注意的地方,特别是slice需要注意的地方比较多.上一篇的最后讲解到创建新的slice时使用第三个索引来限制slice的容量,在操作新slice时,如果新slice的容量大于长度时,添加新元素依然后使源的相应元素改变.这一篇里我会讲解到如何避免这些问题,以及迭代.和做为方法参数方面的知识点. slice的长度和容量设置为同一个值 如果在创建新的slice时我们把

JavaScript的进阶之路(三)引用类型之Object类型和Array类型

引用类型 Object类型 function a(num){ if(num>3){ a(--num); } console.log(num); } a(5); //如何创建对象的实例 var obj1= new Object(); console.log(obj1); obj1.name="吴琼"; obj1.age=28; console.log(obj1.name+" "+obj1.age); //对象字面量语法 ,有点封装的感觉 var obj2 = {

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,