[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 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].

Note:
n is in the range of [1, 106].

思路:

第一反应是挨个求全排列,判断是否符合,结果果断超时,当n=12的时候就已经极限了。

bool isOrder(vector<int>& a,int n)
{
  for(int i =0;i<n;i++)
  {
    if(a[i]== (i+1))return false;
  }
  return true;
}

 int findDerangement2(int n)
 {
   if(n==1)return 0;
   if(n==2)return 1;
   //if(n==3)return 2;
   vector<int> ori(n,0);

   for(int i =0;i<n;i++)ori[i]=i+1;

   long long ret=0;
   while(next_permutation(ori.begin(),ori.end()))
   {
     if( isOrder(ori,n) ) ret++;
   }
  return ret %1000000007;
}

结果想,这么做肯定不多,就想到了用动态规划。

仔细观察,

3的结果为: 2 3 1 与 3 1 2,

求4的结果的时候,将数字4分别与 3的结果每一位进行交换肯定满足题意,即将4与2 3 1和3 1 2每一位交换,得到

4 3 1 2

2 4 1 3

2 3 4 1

4 1 2 3

3 4 2 1

3 1 4 2

一共6个即 dp[3]*3.

还有一种情况就是 3个排列比如

1 X X

X 2 X

X X 3

其中X X是2的排列,将数字 1 2 3分别与4交换,肯定也满足题意 即 d[2]*3

综上就得到 dp[4]= dp[3]*3+d[2]*3;

dp[n] = ((i-1)*dp[i-1]+(i-1)*dp[i-2] )%1000000007;

但是程序不能这么写。。因为((i-1)*dp[i-1]+(i-1)*dp[i-2] )亲测溢出。。。。

改写成dp[n]=(i-1)*(dp[i-1]+dp[i-2])%1000000007;

vector<long long> dp(n+1,0);
   dp[1]=0,dp[2]=1,dp[3]=2;
   for(int i =4;i<=n;i++)
   {
    dp[i] = (i-1)*(dp[i-1]+dp[i-2])%1000000007;
  }
  return dp[n];
时间: 2024-10-24 21:16:17

[leetcode-634-Find the Derangement of An Array]的相关文章

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 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte

LeetCode(26)题解:Remove Duplicates from Sorted Array

https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this

[Leetcode][Python]33: Search in Rotated Sorted Array

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 33: Search in Rotated Sorted Arrayhttps://oj.leetcode.com/problems/search-in-rotated-sorted-array/ Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6

Leetcode 线性表 Remove Duplicates from Sorted Array

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array Total Accepted: 14789 Total Submissions: 46601 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new l

[Leetcode][Python]26: Remove Duplicates from Sorted Array

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 26: Remove Duplicates from Sorted Arrayhttps://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the duplicates in place such that each element appear

leetcode第一刷_Remove Duplicates from Sorted Array II

水题. 我之前说过包含至多几个至少几个的问题都比较难,这个题可是让我大脸了.至多可以重复一次,那就重复次数多于两次再计算重复,否则的话像普通的数据一样直接按照重复次数前移就可以了嘛.不过说归说,这种inspace的思想还是有些用处的,数组这种实现方式致命的缺点就是删除或者添加中间的元素代价太大,因为不好把握数据的最终位置.这个题是一种情况,合并两个排序好的数组也是一个例子. class Solution { public: int removeDuplicates(int A[], int n)

leetcode第一刷_Search in Rotated Sorted Array

旋转数组的查找问题.从头开始扫一遍,O(N)的复杂度,一般也能过,甚至先排序以下,再二分都能过.不过这道题的目的当然不在于此. 想一下旋转之后对我们的查找产生了什么影响.如果没旋转过,我们直接比较target与A[middle]的大小,然后总能非常确定的丢掉源数组的一半,即把搜索空间减半,但是旋转之后,只根据A[middle]是确定不了下一轮的走向的,因为即使A[middle]比target大,按理说我们应该往前找,但是如果源数组是循环左移的,较小的数可能在后半部分. 上面说的都是旋转之后与没旋