在组合数学中,如果一个排列中所有元素都不在原先的位置上,那么这个排列就被称为错位排列。
给定一个从 1 到 n 升序排列的数组,你可以计算出总共有多少个不同的错位排列吗?
由于答案可能非常大,你只需要将答案对 109+7 取余输出即可。
样例 1:
输入: 3
输出: 2
解释: 原始的数组为 [1,2,3]。两个错位排列的数组为 [2,3,1] 和 [3,1,2]。
注释:
n 的范围是 [1, 106]。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-the-derangement-of-an-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路:
排列组合经典错排问题,有公式:
D_(n)=n * D_(n-1) + (-1)^n
参考自:https://zh.wikipedia.org/wiki/错排问题
时间复杂度:O(N)
空间复杂度:O(1)
class Solution(object):
def findDerangement(self, n):
"""
:type n: int
:rtype: int
"""
res = 0
for i in range(n + 1):
res = (i * res + (-1) ** i) % (10 ** 9 + 7)
return res