Skip to content

Commit 6a83374

Browse files
committed
feat: add typescript solution to lc problem: No.0688
No.0688.Knight Probability in Chessboard
1 parent 31f7a06 commit 6a83374

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

solution/0600-0699/0688.Knight Probability in Chessboard/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,33 @@
6666

6767
```
6868

69+
### **TypeScript**
70+
71+
```ts
72+
function knightProbability(n: number, k: number, row: number, column: number): number {
73+
let dp = Array.from({ length: k + 1 }, v => Array.from({ length: n }, w => new Array(n).fill(0)));
74+
const directions = [[-2, -1], [-2, 1], [-1, -2], [-1, 2], [1, -2], [1, 2], [2, -1], [2, 1]];
75+
for (let depth = 0; depth <= k; depth++) {
76+
for (let i = 0; i < n; i++) {
77+
for (let j = 0; j < n; j++) {
78+
if (!depth) {
79+
dp[depth][i][j] = 1;
80+
} else {
81+
for (let [dx, dy] of directions) {
82+
let [x, y] = [i + dx, j + dy];
83+
if (x >= 0 && x < n && y >= 0 && y < n) {
84+
dp[depth][i][j] += dp[depth - 1][x][y] / 8;
85+
}
86+
}
87+
}
88+
89+
}
90+
}
91+
}
92+
return dp[k][row][column];
93+
};
94+
```
95+
6996
### **...**
7097

7198
```

solution/0600-0699/0688.Knight Probability in Chessboard/README_EN.md

+27
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,33 @@ The total probability the knight stays on the board is 0.0625.
6262

6363
```
6464

65+
### **TypeScript**
66+
67+
```ts
68+
function knightProbability(n: number, k: number, row: number, column: number): number {
69+
let dp = Array.from({ length: k + 1 }, v => Array.from({ length: n }, w => new Array(n).fill(0)));
70+
const directions = [[-2, -1], [-2, 1], [-1, -2], [-1, 2], [1, -2], [1, 2], [2, -1], [2, 1]];
71+
for (let depth = 0; depth <= k; depth++) {
72+
for (let i = 0; i < n; i++) {
73+
for (let j = 0; j < n; j++) {
74+
if (!depth) {
75+
dp[depth][i][j] = 1;
76+
} else {
77+
for (let [dx, dy] of directions) {
78+
let [x, y] = [i + dx, j + dy];
79+
if (x >= 0 && x < n && y >= 0 && y < n) {
80+
dp[depth][i][j] += dp[depth - 1][x][y] / 8;
81+
}
82+
}
83+
}
84+
85+
}
86+
}
87+
}
88+
return dp[k][row][column];
89+
};
90+
```
91+
6592
### **...**
6693

6794
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function knightProbability(n: number, k: number, row: number, column: number): number {
2+
let dp = Array.from({ length: k + 1 }, v => Array.from({ length: n }, w => new Array(n).fill(0)));
3+
const directions = [[-2, -1], [-2, 1], [-1, -2], [-1, 2], [1, -2], [1, 2], [2, -1], [2, 1]];
4+
for (let depth = 0; depth <= k; depth++) {
5+
for (let i = 0; i < n; i++) {
6+
for (let j = 0; j < n; j++) {
7+
if (!depth) {
8+
dp[depth][i][j] = 1;
9+
} else {
10+
for (let [dx, dy] of directions) {
11+
let [x, y] = [i + dx, j + dy];
12+
if (x >= 0 && x < n && y >= 0 && y < n) {
13+
dp[depth][i][j] += dp[depth - 1][x][y] / 8;
14+
}
15+
}
16+
}
17+
18+
}
19+
}
20+
}
21+
return dp[k][row][column];
22+
};

0 commit comments

Comments
 (0)