Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aa0b7b3

Browse files
update: 64
1 parent 70bc41c commit aa0b7b3

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
4747
| 57 | [Insert Interval](https://leetcode.com/problems/insert-interval/) | [JavaScript](./src/insert-interval/res.js) | Hard |
4848
| 62 | [Unique Paths](https://leetcode.com/problems/unique-paths/) | [JavaScript](./src/unique-paths/res.js) | Medium |
4949
| 63 | [Unique Paths ii](https://leetcode.com/problems/unique-paths-ii/) | [JavaScript](./src/unique-paths-ii/res.js) | Medium |
50+
| 64 | [minimum-path-sum](https://leetcode.com/problems/minimum-path-sum/) | [TypeScript](./src/minimum-path-sum/res.ts) | Medium |
5051
| 66 | [Plus One](https://leetcode.com/problems/plus-one/) | [JavaScript](./src/plus-one/res.js) | Easy |
5152
| 69 | [Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [JavaScript](./src/sqrtx/res.js) | Easy |
5253
| 71 | [Simplify Path](https://leetcode.com/problems/simplify-path/) | [JavaScript](./src/simplify-path/res.js) | Medium |

‎src/minimum-path-sum/res.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function minPathSum(grid: number[][]): number {
2+
const pathCache: number[][] = [];
3+
4+
function calculate(m:number, n:number): number {
5+
if (m === 0 && n === 0) {
6+
return pathCache[0][0];
7+
}
8+
9+
if (m === 0) {
10+
pathCache[m][n] = (!!pathCache[m][n-1] ? pathCache[m][n-1] : calculate(m,n-1)) + grid[m][n];
11+
} else if (n === 0) {
12+
pathCache[m][n] = (!!pathCache[m-1][n] ? pathCache[m-1][n] : calculate(m-1,n)) + grid[m][n];
13+
} else {
14+
pathCache[m][n] = Math.min(
15+
(!!pathCache[m-1][n] ? pathCache[m-1][n] : calculate(m-1,n)) + grid[m][n],
16+
(!!pathCache[m][n-1] ? pathCache[m][n-1] : calculate(m,n-1)) + grid[m][n],
17+
)
18+
}
19+
20+
return pathCache[m][n];
21+
}
22+
23+
const rowLength = grid.length;
24+
const columnLength = grid[0]?.length;
25+
26+
for (let i = 0; i < rowLength; i++) {
27+
pathCache.push([]);
28+
for (let j = 0; j < columnLength; j++) {
29+
pathCache[i].push(0);
30+
}
31+
}
32+
pathCache[0][0] = grid[0][0];
33+
34+
if (!rowLength || !columnLength) {
35+
return 0;
36+
}
37+
38+
return calculate(rowLength-1, columnLength-1);
39+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /