Skip to content

Commit ed04e34

Browse files
authored
fix: reemove duplicated code (#3820)
1 parent 7c0ac17 commit ed04e34

File tree

2 files changed

+15
-49
lines changed

2 files changed

+15
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
11
function knightDialer(n: number): number {
2-
const MOD: number = 1e9 + 7;
3-
4-
if (n === 1) {
5-
return 10;
2+
const mod = 1e9 + 7;
3+
const f: number[] = Array(10).fill(1);
4+
while (--n) {
5+
const g: number[] = Array(10).fill(0);
6+
g[0] = (f[4] + f[6]) % mod;
7+
g[1] = (f[6] + f[8]) % mod;
8+
g[2] = (f[7] + f[9]) % mod;
9+
g[3] = (f[4] + f[8]) % mod;
10+
g[4] = (f[0] + f[3] + f[9]) % mod;
11+
g[6] = (f[0] + f[1] + f[7]) % mod;
12+
g[7] = (f[2] + f[6]) % mod;
13+
g[8] = (f[1] + f[3]) % mod;
14+
g[9] = (f[2] + f[4]) % mod;
15+
f.splice(0, 10, ...g);
616
}
7-
8-
const f: number[] = new Array(10).fill(1);
9-
10-
while (--n > 0) {
11-
const t: number[] = new Array(10).fill(0);
12-
13-
t[0] = f[4] + f[6];
14-
t[1] = f[6] + f[8];
15-
t[2] = f[7] + f[9];
16-
t[3] = f[4] + f[8];
17-
t[4] = f[0] + f[3] + f[9];
18-
t[6] = f[0] + f[1] + f[7];
19-
t[7] = f[2] + f[6];
20-
t[8] = f[1] + f[3];
21-
t[9] = f[2] + f[4];
22-
23-
for (let i = 0; i < 10; ++i) {
24-
f[i] = t[i] % MOD;
25-
}
26-
}
27-
28-
let ans: number = 0;
29-
for (const v of f) {
30-
ans = (ans + v) % MOD;
31-
}
32-
33-
return ans;
17+
return f.reduce((a, b) => (a + b) % mod);
3418
}

solution/0900-0999/0935.Knight Dialer/solution.ts

-18
This file was deleted.

0 commit comments

Comments
 (0)