-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path634.cpp
More file actions
27 lines (27 loc) · 907 Bytes
/
634.cpp
File metadata and controls
27 lines (27 loc) · 907 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
__________________________________________________________________________________________________
class Solution {
public:
int findDerangement(int n) {
if (n < 2) return 0;
vector<long long> dp(n + 1, 0);
dp[1] = 0; dp[2] = 1;
for (int i = 3; i <= n; ++i) {
dp[i] = (dp[i - 1] + dp[i - 2]) * (i - 1) % 1000000007;
}
return dp[n];
}
};
__________________________________________________________________________________________________
class Solution {
public:
int findDerangement(int n) {
if (n < 2) return 0;
vector<long long> dp(n + 1, 0);
dp[1] = 0; dp[2] = 1;
for (int i = 3; i <= n; ++i) {
dp[i] = (dp[i - 1] + dp[i - 2]) * (i - 1) % 1000000007;
}
return dp[n];
}
};
__________________________________________________________________________________________________