forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
61 lines (46 loc) · 1.28 KB
/
main2.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
/// Source : https://leetcode.com/problems/count-vowels-permutation/
/// Author : liuyubobobo
/// Time : 2019-10-06
#include <iostream>
#include <vector>
using namespace std;
/// Dynamic Programming
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
private:
int MOD = 1e9 + 7;
vector<vector<int>> g = {
//a e i o u
{0, 1, 0, 0, 0}, // a
{1, 0, 1, 0, 0}, // e
{1, 1, 0, 1, 1}, // i
{0, 0, 1, 0, 1}, // o
{1, 0, 0, 0, 0} // u
};
public:
int countVowelPermutation(int n) {
vector<vector<int>> dp(5, vector<int>(n, 0));
for(int i = 0; i < 5; i ++)
dp[i][n - 1] = 1;
for(int j = n - 2; j >= 0; j --)
for(int i = 0; i < 5; i ++){
for(int k = 0; k < 5; k ++)
if(g[i][k])
dp[i][j] += dp[k][j + 1], dp[i][j] %= MOD;
}
int res = 0;
for(int i = 0; i < 5; i ++)
res += dp[i][0], res %= MOD;
return res;
}
};
int main() {
cout << Solution().countVowelPermutation(1) << endl;
// 5
cout << Solution().countVowelPermutation(2) << endl;
// 10
cout << Solution().countVowelPermutation(5) << endl;
// 68
return 0;
}