Skip to content

Commit 3804c17

Browse files
committed
feat: add typescript solution to lc problem: No.2435
No.2435.Paths in Matrix Whose Sum Is Divisible by K
1 parent 976a940 commit 3804c17

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

solution/2400-2499/2435.Paths in Matrix Whose Sum Is Divisible by K/README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,21 @@ func numberOfPaths(grid [][]int, k int) int {
324324
### **TypeScript**
325325

326326
```ts
327-
327+
function numberOfPaths(grid: number[][], k: number): number {
328+
const MOD = 10 ** 9 + 7;
329+
const m = grid.length, n = grid[0].length;
330+
let ans = Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => new Array(k).fill(0)));
331+
ans[0][1][0] = 1;
332+
for (let i = 0; i < m; i++) {
333+
for (let j = 0; j < n; j++) {
334+
for (let v = 0; v < k; v++) {
335+
let key = (grid[i][j] + v) % k;
336+
ans[i + 1][j + 1][key] = (ans[i][j + 1][v] + ans[i + 1][j][v] + ans[i + 1][j + 1][key]) % MOD;
337+
}
338+
}
339+
}
340+
return ans[m][n][0];
341+
};
328342
```
329343

330344
### **...**

solution/2400-2499/2435.Paths in Matrix Whose Sum Is Divisible by K/README_EN.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,21 @@ func numberOfPaths(grid [][]int, k int) int {
285285
### **TypeScript**
286286

287287
```ts
288-
288+
function numberOfPaths(grid: number[][], k: number): number {
289+
const MOD = 10 ** 9 + 7;
290+
const m = grid.length, n = grid[0].length;
291+
let ans = Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => new Array(k).fill(0)));
292+
ans[0][1][0] = 1;
293+
for (let i = 0; i < m; i++) {
294+
for (let j = 0; j < n; j++) {
295+
for (let v = 0; v < k; v++) {
296+
let key = (grid[i][j] + v) % k;
297+
ans[i + 1][j + 1][key] = (ans[i][j + 1][v] + ans[i + 1][j][v] + ans[i + 1][j + 1][key]) % MOD;
298+
}
299+
}
300+
}
301+
return ans[m][n][0];
302+
};
289303
```
290304

291305
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function numberOfPaths(grid: number[][], k: number): number {
2+
const MOD = 10 ** 9 + 7;
3+
const m = grid.length, n = grid[0].length;
4+
let ans = Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => new Array(k).fill(0)));
5+
ans[0][1][0] = 1;
6+
for (let i = 0; i < m; i++) {
7+
for (let j = 0; j < n; j++) {
8+
for (let v = 0; v < k; v++) {
9+
let key = (grid[i][j] + v) % k;
10+
ans[i + 1][j + 1][key] = (ans[i][j + 1][v] + ans[i + 1][j][v] + ans[i + 1][j + 1][key]) % MOD;
11+
}
12+
}
13+
}
14+
return ans[m][n][0];
15+
};

0 commit comments

Comments
 (0)