Skip to content

Commit 6b734ea

Browse files
committed
1220
1 parent 0ebd588 commit 6b734ea

File tree

4 files changed

+269
-0
lines changed

4 files changed

+269
-0
lines changed

leetcode/1220/0.in

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
5
2+
a e
3+
e ai
4+
i aeou
5+
o iu
6+
u a
7+
8+
[0, 1, 1, 0, 1]
9+
[1, 0, 1, 0, 0]
10+
[0, 1, 0, 1, 0]
11+
[0, 0, 1, 0, 0]
12+
[0, 0, 1, 1, 0]

leetcode/1220/main.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
3+
*/
4+
5+
#include <bits/stdc++.h>
6+
7+
#include "../../lib/LeetCodeInputTemplate.hpp"
8+
#include "solution.cpp"
9+
using namespace std;
10+
11+
typedef vector<int> vi;
12+
typedef vector<vector<int>> vvi;
13+
14+
int main() {
15+
LeetCodeInput li("0.in");
16+
17+
int i = 0;
18+
auto l0 = li.get<int>(i++);
19+
20+
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
21+
Solution solution;
22+
auto output = solution.countVowelPermutation(5);
23+
output = solution.countVowelPermutation(2);
24+
output = solution.countVowelPermutation(2);
25+
chrono::steady_clock::time_point end = chrono::steady_clock::now();
26+
cout << "Time difference = "
27+
<< chrono::duration_cast<chrono::microseconds>(end - begin).count()
28+
<< "µs" << endl;
29+
30+
cout << output << endl;
31+
32+
// for (int i: output){
33+
// cout << i << ' ';
34+
// }
35+
36+
// for (auto i: output){
37+
// for (int j: i){
38+
// cout << j << ' ';
39+
// }
40+
// cout << '\n';
41+
// }
42+
43+
return 0;
44+
}

leetcode/1220/main.ipynb

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"an O(1) algorithm is possible using spectrum decomposition, that is, when pow(n) is an O(1) operation."
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 86,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import numpy as np\n",
17+
"\n",
18+
"A = np.array([\n",
19+
" [0, 1, 1, 0, 1],\n",
20+
" [1, 0, 1, 0, 0],\n",
21+
" [0, 1, 0, 1, 0],\n",
22+
" [0, 0, 1, 0, 0],\n",
23+
" [0, 0, 1, 1, 0]\n",
24+
"])\n",
25+
"\n",
26+
"w, v = np.linalg.eig(A)"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 93,
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"x = np.linalg.solve(v, np.array([1,1,1,1,1]))"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 94,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"def solution(n):\n",
45+
" n -= 1\n",
46+
" res = 0\n",
47+
" for i, eigenval in enumerate(w):\n",
48+
" power = pow(eigenval, n)\n",
49+
" for j in range(5):\n",
50+
" res += power * x[i] * v[j,i]\n",
51+
" return res"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 95,
57+
"metadata": {},
58+
"outputs": [
59+
{
60+
"name": "stdout",
61+
"output_type": "stream",
62+
"text": [
63+
"(68.00000000000007-1.2836953722228372e-15j)\n"
64+
]
65+
}
66+
],
67+
"source": [
68+
"print(solution(5))"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": []
77+
}
78+
],
79+
"metadata": {
80+
"kernelspec": {
81+
"display_name": "Python 3.10.4 ('data')",
82+
"language": "python",
83+
"name": "python3"
84+
},
85+
"language_info": {
86+
"codemirror_mode": {
87+
"name": "ipython",
88+
"version": 3
89+
},
90+
"file_extension": ".py",
91+
"mimetype": "text/x-python",
92+
"name": "python",
93+
"nbconvert_exporter": "python",
94+
"pygments_lexer": "ipython3",
95+
"version": "3.10.4"
96+
},
97+
"orig_nbformat": 4,
98+
"vscode": {
99+
"interpreter": {
100+
"hash": "ba616ce64a963caf1bdcb2b13e3946f6787097ec1065511c2582f9ec4f8f3178"
101+
}
102+
}
103+
},
104+
"nbformat": 4,
105+
"nbformat_minor": 2
106+
}

leetcode/1220/solution.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
3+
time: O(n)
4+
space: O(n)
5+
6+
Runtime
7+
0 ms
8+
Beats
9+
100%
10+
Memory
11+
6.4 MB
12+
Beats
13+
69.7%
14+
*/
15+
16+
#include <bits/stdc++.h>
17+
18+
using namespace std;
19+
20+
#define all(x) begin(x), end(x)
21+
typedef vector<int> vi;
22+
typedef vector<vector<int>> vvi;
23+
24+
constexpr int MOD = 1e9 + 7;
25+
constexpr int NMAX = 2e4 + 1;
26+
27+
int a[NMAX];
28+
int e[NMAX];
29+
int i[NMAX];
30+
int o[NMAX];
31+
int u[NMAX];
32+
int calced = 1;
33+
34+
class Solution {
35+
private:
36+
inline void add_mod(int& a, int& b) {
37+
a += b;
38+
a %= MOD;
39+
};
40+
41+
inline int get_res(int n) {
42+
int res = 0;
43+
add_mod(res, a[n]);
44+
add_mod(res, e[n]);
45+
add_mod(res, i[n]);
46+
add_mod(res, o[n]);
47+
add_mod(res, u[n]);
48+
return res;
49+
}
50+
51+
public:
52+
Solution() {
53+
a[1] = 1;
54+
e[1] = 1;
55+
i[1] = 1;
56+
o[1] = 1;
57+
u[1] = 1;
58+
}
59+
60+
int countVowelPermutation(int n) {
61+
if (n > calced) {
62+
for (int x = calced + 1; x <= n; ++x) {
63+
int y = x - 1;
64+
65+
// a[x] = e[y] + i[y] + u[y];
66+
// e[x] = a[y] + i[y];
67+
// i[x] = e[y] + o[y];
68+
// o[x] = i[y];
69+
// u[x] = i[y] + o[y];
70+
71+
/// this can be omited but dangerous; without this, should calced be a
72+
/// class member, a new solution (for leetcode parallel) would use the
73+
/// same mem address as before, causing a[x] to have correct value
74+
/// before calc, causing the result to be doubled
75+
a[x] = 0;
76+
e[x] = 0;
77+
i[x] = 0;
78+
o[x] = 0;
79+
u[x] = 0;
80+
81+
add_mod(a[x], e[y]);
82+
add_mod(a[x], i[y]);
83+
add_mod(a[x], u[y]);
84+
85+
add_mod(e[x], a[y]);
86+
add_mod(e[x], i[y]);
87+
88+
add_mod(i[x], e[y]);
89+
add_mod(i[x], o[y]);
90+
91+
add_mod(o[x], i[y]);
92+
93+
add_mod(u[x], i[y]);
94+
add_mod(u[x], o[y]);
95+
}
96+
calced = n;
97+
}
98+
return get_res(n);
99+
}
100+
};
101+
102+
const static auto initialize = [] {
103+
std::ios::sync_with_stdio(false);
104+
std::cin.tie(nullptr);
105+
std::cout.tie(nullptr);
106+
return nullptr;
107+
}();

0 commit comments

Comments
 (0)