Skip to content

Commit 6f950c9

Browse files
authored
feat: add solutions to lc problem: No.1220 (doocs#1612)
No.1220.Count Vowels Permutation
1 parent 6f06972 commit 6f950c9

File tree

8 files changed

+923
-207
lines changed

8 files changed

+923
-207
lines changed

solution/1200-1299/1220.Count Vowels Permutation/README.md

+359-64
Large diffs are not rendered by default.

solution/1200-1299/1220.Count Vowels Permutation/README_EN.md

+337-63
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
1-
class Solution {
2-
public:
3-
int countVowelPermutation(int n) {
4-
using ll = long long;
5-
const ll mod = 1e9 + 7;
6-
vector<ll> dp(5, 1);
7-
vector<ll> t(5);
8-
for (int i = 0; i < n - 1; ++i) {
9-
t[0] = (dp[1] + dp[2] + dp[4]) % mod;
10-
t[1] = (dp[0] + dp[2]) % mod;
11-
t[2] = (dp[1] + dp[3]) % mod;
12-
t[3] = dp[2];
13-
t[4] = (dp[2] + dp[3]) % mod;
14-
dp = t;
15-
}
16-
return accumulate(dp.begin(), dp.end(), 0LL) % mod;
17-
}
1+
class Solution {
2+
public:
3+
int countVowelPermutation(int n) {
4+
vector<vector<ll>> a = {
5+
{0, 1, 0, 0, 0},
6+
{1, 0, 1, 0, 0},
7+
{1, 1, 0, 1, 1},
8+
{0, 0, 1, 0, 1},
9+
{1, 0, 0, 0, 0}};
10+
vector<vector<ll>> res = pow(a, n - 1);
11+
return accumulate(res[0].begin(), res[0].end(), 0LL) % mod;
12+
}
13+
14+
private:
15+
using ll = long long;
16+
const int mod = 1e9 + 7;
17+
18+
vector<vector<ll>> mul(vector<vector<ll>>& a, vector<vector<ll>>& b) {
19+
int m = a.size(), n = b[0].size();
20+
vector<vector<ll>> c(m, vector<ll>(n));
21+
for (int i = 0; i < m; ++i) {
22+
for (int j = 0; j < n; ++j) {
23+
for (int k = 0; k < b.size(); ++k) {
24+
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
25+
}
26+
}
27+
}
28+
return c;
29+
}
30+
31+
vector<vector<ll>> pow(vector<vector<ll>>& a, int n) {
32+
vector<vector<ll>> res;
33+
res.push_back({1, 1, 1, 1, 1});
34+
while (n) {
35+
if (n & 1) {
36+
res = mul(res, a);
37+
}
38+
a = mul(a, a);
39+
n >>= 1;
40+
}
41+
return res;
42+
}
1843
};
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
1-
func countVowelPermutation(n int) int {
2-
const mod int = 1e9 + 7
3-
dp := [5]int{1, 1, 1, 1, 1}
4-
for i := 0; i < n-1; i++ {
5-
dp = [5]int{
6-
(dp[1] + dp[2] + dp[4]) % mod,
7-
(dp[0] + dp[2]) % mod,
8-
(dp[1] + dp[3]) % mod,
9-
dp[2],
10-
(dp[2] + dp[3]) % mod,
1+
const mod = 1e9 + 7
2+
3+
func countVowelPermutation(n int) (ans int) {
4+
a := [][]int{
5+
{0, 1, 0, 0, 0},
6+
{1, 0, 1, 0, 0},
7+
{1, 1, 0, 1, 1},
8+
{0, 0, 1, 0, 1},
9+
{1, 0, 0, 0, 0}}
10+
res := pow(a, n-1)
11+
for _, x := range res[0] {
12+
ans = (ans + x) % mod
13+
}
14+
return
15+
}
16+
17+
func mul(a, b [][]int) [][]int {
18+
m, n := len(a), len(b[0])
19+
c := make([][]int, m)
20+
for i := range c {
21+
c[i] = make([]int, n)
22+
}
23+
for i := 0; i < m; i++ {
24+
for j := 0; j < n; j++ {
25+
for k := 0; k < len(b); k++ {
26+
c[i][j] = (c[i][j] + a[i][k]*b[k][j]) % mod
27+
}
1128
}
1229
}
13-
ans := 0
14-
for _, v := range dp {
15-
ans = (ans + v) % mod
30+
return c
31+
}
32+
33+
func pow(a [][]int, n int) [][]int {
34+
res := [][]int{{1, 1, 1, 1, 1}}
35+
for n > 0 {
36+
if n&1 == 1 {
37+
res = mul(res, a)
38+
}
39+
a = mul(a, a)
40+
n >>= 1
1641
}
17-
return ans
42+
return res
1843
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
1-
class Solution {
2-
private static final long MOD = (long) 1e9 + 7;
3-
4-
public int countVowelPermutation(int n) {
5-
long[] dp = new long[5];
6-
long[] t = new long[5];
7-
Arrays.fill(dp, 1);
8-
for (int i = 0; i < n - 1; ++i) {
9-
t[0] = (dp[1] + dp[2] + dp[4]) % MOD;
10-
t[1] = (dp[0] + dp[2]) % MOD;
11-
t[2] = (dp[1] + dp[3]) % MOD;
12-
t[3] = dp[2];
13-
t[4] = (dp[2] + dp[3]) % MOD;
14-
System.arraycopy(t, 0, dp, 0, 5);
15-
}
16-
long ans = 0;
17-
for (int i = 0; i < 5; ++i) {
18-
ans = (ans + dp[i]) % MOD;
19-
}
20-
return (int) ans;
21-
}
1+
class Solution {
2+
private final int mod = (int) 1e9 + 7;
3+
4+
public int countVowelPermutation(int n) {
5+
long[][] a
6+
= {{0, 1, 0, 0, 0}, {1, 0, 1, 0, 0}, {1, 1, 0, 1, 1}, {0, 0, 1, 0, 1}, {1, 0, 0, 0, 0}};
7+
long[][] res = pow(a, n - 1);
8+
long ans = 0;
9+
for (long x : res[0]) {
10+
ans = (ans + x) % mod;
11+
}
12+
return (int) ans;
13+
}
14+
15+
private long[][] mul(long[][] a, long[][] b) {
16+
int m = a.length, n = b[0].length;
17+
long[][] c = new long[m][n];
18+
for (int i = 0; i < m; ++i) {
19+
for (int j = 0; j < n; ++j) {
20+
for (int k = 0; k < b.length; ++k) {
21+
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
22+
}
23+
}
24+
}
25+
return c;
26+
}
27+
28+
private long[][] pow(long[][] a, int n) {
29+
long[][] res = new long[1][a.length];
30+
Arrays.fill(res[0], 1);
31+
while (n > 0) {
32+
if ((n & 1) == 1) {
33+
res = mul(res, a);
34+
}
35+
a = mul(a, a);
36+
n >>= 1;
37+
}
38+
return res;
39+
}
2240
}

solution/1200-1299/1220.Count Vowels Permutation/Solution.js

+37-15
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,43 @@
22
* @param {number} n
33
* @return {number}
44
*/
5+
6+
const mod = 1e9 + 7;
7+
58
var countVowelPermutation = function (n) {
6-
const mod = 1000000007;
7-
const dp = new Array(5).fill(1);
8-
const t = new Array(5).fill(0);
9-
for (let i = 0; i < n - 1; ++i) {
10-
t[0] = (dp[1] + dp[2] + dp[4]) % mod;
11-
t[1] = (dp[0] + dp[2]) % mod;
12-
t[2] = (dp[1] + dp[3]) % mod;
13-
t[3] = dp[2];
14-
t[4] = (dp[2] + dp[3]) % mod;
15-
dp.splice(0, 5, ...t);
9+
const a = [
10+
[0, 1, 0, 0, 0],
11+
[1, 0, 1, 0, 0],
12+
[1, 1, 0, 1, 1],
13+
[0, 0, 1, 0, 1],
14+
[1, 0, 0, 0, 0],
15+
];
16+
const res = pow(a, n - 1);
17+
return res[0].reduce((a, b) => (a + b) % mod);
18+
};
19+
20+
function mul(a, b) {
21+
const [m, n] = [a.length, b[0].length];
22+
const c = Array.from({ length: m }, () => Array.from({ length: n }, () => 0));
23+
for (let i = 0; i < m; ++i) {
24+
for (let j = 0; j < n; ++j) {
25+
for (let k = 0; k < b.length; ++k) {
26+
c[i][j] =
27+
(c[i][j] + Number((BigInt(a[i][k]) * BigInt(b[k][j])) % BigInt(mod))) % mod;
28+
}
29+
}
1630
}
17-
let ans = 0;
18-
for (let i = 0; i < 5; ++i) {
19-
ans = (ans + dp[i]) % mod;
31+
return c;
32+
}
33+
34+
function pow(a, n) {
35+
let res = [[1, 1, 1, 1, 1]];
36+
while (n) {
37+
if (n & 1) {
38+
res = mul(res, a);
39+
}
40+
a = mul(a, a);
41+
n >>>= 1;
2042
}
21-
return ans;
22-
};
43+
return res;
44+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1-
class Solution:
2-
def countVowelPermutation(self, n: int) -> int:
3-
dp = (1, 1, 1, 1, 1)
4-
MOD = 1000000007
5-
for _ in range(n - 1):
6-
dp = (
7-
(dp[1] + dp[2] + dp[4]) % MOD,
8-
(dp[0] + dp[2]) % MOD,
9-
(dp[1] + dp[3]) % MOD,
10-
dp[2],
11-
(dp[2] + dp[3]) % MOD,
12-
)
13-
return sum(dp) % MOD
1+
class Solution:
2+
def countVowelPermutation(self, n: int) -> int:
3+
mod = 10**9 + 7
4+
5+
def mul(a: List[List[int]], b: List[List[int]]) -> List[List[int]]:
6+
m, n = len(a), len(b[0])
7+
c = [[0] * n for _ in range(m)]
8+
for i in range(m):
9+
for j in range(n):
10+
for k in range(len(b)):
11+
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod
12+
return c
13+
14+
def pow(a: List[List[int]], n: int) -> List[int]:
15+
res = [[1] * len(a)]
16+
while n:
17+
if n & 1:
18+
res = mul(res, a)
19+
a = mul(a, a)
20+
n >>= 1
21+
return res
22+
23+
a = [
24+
[0, 1, 0, 0, 0],
25+
[1, 0, 1, 0, 0],
26+
[1, 1, 0, 1, 1],
27+
[0, 0, 1, 0, 1],
28+
[1, 0, 0, 0, 0],
29+
]
30+
res = pow(a, n - 1)
31+
return sum(map(sum, res)) % mod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const mod = 1e9 + 7;
2+
3+
function countVowelPermutation(n: number): number {
4+
const a: number[][] = [
5+
[0, 1, 0, 0, 0],
6+
[1, 0, 1, 0, 0],
7+
[1, 1, 0, 1, 1],
8+
[0, 0, 1, 0, 1],
9+
[1, 0, 0, 0, 0],
10+
];
11+
const res = pow(a, n - 1);
12+
return res[0].reduce((a, b) => (a + b) % mod);
13+
}
14+
15+
function mul(a: number[][], b: number[][]): number[][] {
16+
const [m, n] = [a.length, b[0].length];
17+
const c = Array.from({ length: m }, () => Array.from({ length: n }, () => 0));
18+
for (let i = 0; i < m; ++i) {
19+
for (let j = 0; j < n; ++j) {
20+
for (let k = 0; k < b.length; ++k) {
21+
c[i][j] =
22+
(c[i][j] + Number((BigInt(a[i][k]) * BigInt(b[k][j])) % BigInt(mod))) % mod;
23+
}
24+
}
25+
}
26+
return c;
27+
}
28+
29+
function pow(a: number[][], n: number): number[][] {
30+
let res: number[][] = [[1, 1, 1, 1, 1]];
31+
while (n) {
32+
if (n & 1) {
33+
res = mul(res, a);
34+
}
35+
a = mul(a, a);
36+
n >>>= 1;
37+
}
38+
return res;
39+
}

0 commit comments

Comments
 (0)