forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-vowels-permutation.cpp
75 lines (69 loc) · 2.56 KB
/
count-vowels-permutation.cpp
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Time: O(logn)
// Space: O(1)
class Solution {
public:
int countVowelPermutation(int n) {
vector<vector<int>> T = {{0, 1, 1, 0, 1},
{1, 0, 1, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 1, 0, 0},
{0, 0, 1, 1, 0}};
const auto& result = matrixExpo(T, n - 1);
return accumulate(result.cbegin(), result.cend(), 0,
[&](int total, const vector<int>& row) {
return (total +
accumulate(row.cbegin(), row.cend(), 0,
[&](int sum, int val) {
return (sum + val) % MOD;
})) % MOD;
});
}
private:
vector<vector<int>> matrixExpo(const vector<vector<int>>& A, int pow) {
vector<vector<int>> result(A.size(), vector<int>(A.size()));
vector<vector<int>> A_exp(A);
for (int i = 0; i < A.size(); ++i) {
result[i][i] = 1;
}
while (pow) {
if (pow % 2 == 1) {
result = matrixMult(result, A_exp);
}
A_exp = matrixMult(A_exp, A_exp);
pow /= 2;
}
return result;
}
vector<vector<int>> matrixMult(const vector<vector<int>>& A, const vector<vector<int>>& B) {
vector<vector<int>> result(A.size(), vector<int>(B[0].size()));
for (int i = 0; i < A.size(); ++i) {
for (int j = 0; j < B[0].size(); ++j) {
int64_t entry = 0;
for (int k = 0; k < B.size(); ++k) {
entry = (static_cast<int64_t>(A[i][k]) * B[k][j] % MOD + entry) % MOD;
}
result[i][j] = static_cast<int>(entry);
}
}
return result;
}
const int MOD = 1e9 + 7;
};
// Time: O(n)
// Space: O(1)
class Solution2 {
public:
int countVowelPermutation(int n) {
int a = 1, e = 1, i = 1, o = 1, u = 1;
for (int x = 1; x < n; ++x) {
tie(a, e, i, o, u) = make_tuple(((e + i) % MOD + u) % MOD,
(a + i) % MOD,
(e + o) % MOD,
i,
(i + o) % MOD);
}
return (((a + e) % MOD + (i + o) % MOD) % MOD + u) % MOD;
}
private:
const int MOD = 1e9 + 7;
};