[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 =;i<n;i++)
{
if(a[i]== (i+))return false;
}
return true;
} int findDerangement2(int n)
{
if(n==)return ;
if(n==)return ;
//if(n==3)return 2;
vector<int> ori(n,); for(int i =;i<n;i++)ori[i]=i+; long long ret=;
while(next_permutation(ori.begin(),ori.end()))
{
if( isOrder(ori,n) ) ret++;
}
return ret %;
}
结果想,这么做肯定不多,就想到了用动态规划。
仔细观察,
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+,);
dp[]=,dp[]=,dp[]=;
for(int i =;i<=n;i++)
{
dp[i] = (i-)*(dp[i-]+dp[i-])%;
}
return dp[n];
[leetcode-634-Find the Derangement of An Array]的更多相关文章
- [Leetcode][Python]33: Search in Rotated Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- 【一天一道LeetCode】#81. Search in Rotated Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array
乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array 一.前言 将传统的问题进行一些稍微的变形,这个时候我们可能无所适从了,因此还是实践出真知, ...
- 乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array
乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array 一.前言 我们这次的实验是去除重复的有序数组元素,有大体两种算法. 二.Remo ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- LeetCode——Find All Numbers Disappeared in an Array
LeetCode--Find All Numbers Disappeared in an Array Question Given an array of integers where 1 ≤ a[i ...
- LeetCode(26)题解:Remove Duplicates from Sorted Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
随机推荐
- mobileeye
if a human can drive a car based on vision alone – so can a computer. 但是目前哪家能做到?
- 使用TestFlight测试时候相关内容
前言:记录一下使用TestFlight测试时候相关内容 场景:在我们添加测试员:给测试员发送了邀请:测试员使用TestFlight的时候,其实是有崩溃的次数的记录的,相应的崩溃的信息也是可以查询到的. ...
- LeetCode11.盛最多水的容器 JavaScript
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- linux ccenteros 部署 redis
step one : yum install redis -- 安装redis数据库 step two:安装完成之后开启redis 服务 service redis start syste ...
- code ELIFECYCLE (代码周期)
问题:build 不成功 解决:新建一个dist 文件,没有自动新建dist 文件 问题 :npm run dev 时候 解决:
- [JSOI2008]最大数(线段树基础)
题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制: L 不超过当前数列的长度.(L > ...
- atan和atan2反正切计算
typedef struct point { double x, y; }point; //给定两个点 point a(x1,y1),b(x2,y2); 使用反三角函数atan求斜率,原型如下 flo ...
- 【TOJ 5065】最长连续子序列(前缀和)
Description 给定一系列非负整数,求最长的连续子序列,使其和是7的倍数. Input 第一行为正整数N(1<=N<=50000),接下来有N行,每行有一个非负整数,所有整数不大于 ...
- springboot整合swagger笔记
首先,在pom.xml中添加依赖 <!--swagger--> <dependency> <groupId>io.springfox</groupId> ...
- 三、css篇
#这里强烈推荐一本书<css世界>,css第一书. #上面的层叠顺序得记住. 1.align-items justify-content 是flex(弹性盒模型)必须要会的属性,alig ...