Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ts solution to lc problem: No.935 #2025

Merged
merged 9 commits into from
Nov 27, 2023
39 changes: 39 additions & 0 deletions solution/0900-0999/0935.Knight Dialer/README.md
Original file line number Diff line number Diff line change
@@ -224,6 +224,45 @@ public class Solution {
}
```

### **TypeScript**

```ts
function knightDialer(n: number): number {
const MOD: number = 1e9 + 7;

if (n === 1) {
return 10;
}

let f: number[] = new Array(10).fill(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const f: number[] = Array(10).fill(1);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated


while (--n > 0) {
let t: number[] = new Array(10).fill(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const t: number[] = Array(10).fill(0);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated


t[0] = f[4] + f[6];
t[1] = f[6] + f[8];
t[2] = f[7] + f[9];
t[3] = f[4] + f[8];
t[4] = f[0] + f[3] + f[9];
t[6] = f[0] + f[1] + f[7];
t[7] = f[2] + f[6];
t[8] = f[1] + f[3];
t[9] = f[2] + f[4];

for (let i = 0; i < 10; ++i) {
f[i] = t[i] % MOD;
}
}

let ans: number = 0;
for (let v of f) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(const v of f)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

ans = (ans + v) % MOD;
}

return ans;
}
```

### **...**

```
39 changes: 39 additions & 0 deletions solution/0900-0999/0935.Knight Dialer/README_EN.md
Original file line number Diff line number Diff line change
@@ -205,6 +205,45 @@ public class Solution {
}
```

### **TypeScript**

```ts
function knightDialer(n: number): number {
const MOD: number = 1e9 + 7;

if (n === 1) {
return 10;
}

let f: number[] = new Array(10).fill(1);

while (--n > 0) {
let t: number[] = new Array(10).fill(0);

t[0] = f[4] + f[6];
t[1] = f[6] + f[8];
t[2] = f[7] + f[9];
t[3] = f[4] + f[8];
t[4] = f[0] + f[3] + f[9];
t[6] = f[0] + f[1] + f[7];
t[7] = f[2] + f[6];
t[8] = f[1] + f[3];
t[9] = f[2] + f[4];

for (let i = 0; i < 10; ++i) {
f[i] = t[i] % MOD;
}
}

let ans: number = 0;
for (let v of f) {
ans = (ans + v) % MOD;
}

return ans;
}
```

### **...**

```
34 changes: 34 additions & 0 deletions solution/0900-0999/0935.Knight Dialer/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function knightDialer(n: number): number {
const MOD: number = 1e9 + 7;

if (n === 1) {
return 10;
}

let f: number[] = new Array(10).fill(1);

while (--n > 0) {
let t: number[] = new Array(10).fill(0);

t[0] = f[4] + f[6];
t[1] = f[6] + f[8];
t[2] = f[7] + f[9];
t[3] = f[4] + f[8];
t[4] = f[0] + f[3] + f[9];
t[6] = f[0] + f[1] + f[7];
t[7] = f[2] + f[6];
t[8] = f[1] + f[3];
t[9] = f[2] + f[4];

for (let i = 0; i < 10; ++i) {
f[i] = t[i] % MOD;
}
}

let ans: number = 0;
for (let v of f) {
ans = (ans + v) % MOD;
}

return ans;
}