Skip to content

Commit 4cf9c86

Browse files
committed
feat: add typescript solution to lc problem: No.2304
No.2304.Minimum Path Cost in a Grid
1 parent 9fc131f commit 4cf9c86

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

solution/2300-2399/2304.Minimum Path Cost in a Grid/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,24 @@ impl Solution {
221221
### **TypeScript**
222222

223223
```ts
224-
224+
function minPathCost(grid: number[][], moveCost: number[][]): number {
225+
const m = grid.length, n = grid[0].length;
226+
let pre = grid[0].slice();
227+
for (let i = 1; i < m; i++) {
228+
let next = new Array(n);
229+
for (let j = 0; j < n; j++) {
230+
const key = grid[i - 1][j];
231+
for (let k = 0; k < n; k++) {
232+
let sum = pre[j] + moveCost[key][k] + grid[i][k];
233+
if (j == 0 || next[k] > sum) {
234+
next[k] = sum;
235+
}
236+
}
237+
}
238+
pre = next;
239+
}
240+
return Math.min(...pre);
241+
};
225242
```
226243

227244
### **...**

solution/2300-2399/2304.Minimum Path Cost in a Grid/README_EN.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,24 @@ impl Solution {
207207
### **TypeScript**
208208

209209
```ts
210-
210+
function minPathCost(grid: number[][], moveCost: number[][]): number {
211+
const m = grid.length, n = grid[0].length;
212+
let pre = grid[0].slice();
213+
for (let i = 1; i < m; i++) {
214+
let next = new Array(n);
215+
for (let j = 0; j < n; j++) {
216+
const key = grid[i - 1][j];
217+
for (let k = 0; k < n; k++) {
218+
let sum = pre[j] + moveCost[key][k] + grid[i][k];
219+
if (j == 0 || next[k] > sum) {
220+
next[k] = sum;
221+
}
222+
}
223+
}
224+
pre = next;
225+
}
226+
return Math.min(...pre);
227+
};
211228
```
212229

213230
### **...**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function minPathCost(grid: number[][], moveCost: number[][]): number {
2+
const m = grid.length, n = grid[0].length;
3+
let pre = grid[0].slice();
4+
for (let i = 1; i < m; i++) {
5+
let next = new Array(n);
6+
for (let j = 0; j < n; j++) {
7+
const key = grid[i - 1][j];
8+
for (let k = 0; k < n; k++) {
9+
let sum = pre[j] + moveCost[key][k] + grid[i][k];
10+
if (j == 0 || next[k] > sum) {
11+
next[k] = sum;
12+
}
13+
}
14+
}
15+
pre = next;
16+
}
17+
return Math.min(...pre);
18+
};

0 commit comments

Comments
 (0)