Skip to content

Commit

Permalink
feat: 增加 64. 最小路径和
Browse files Browse the repository at this point in the history
  • Loading branch information
fxss5201 committed Aug 24, 2024
1 parent 6feb1fe commit ae3f0f3
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/.vitepress/leetcodeItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ const leetcodeItems = [
"text": "63. 不同路径 II",
"link": "/leetcode/uniquePathsIi"
},
{
"text": "64. 最小路径和",
"link": "/leetcode/minimumPathSum"
},
{
"text": "83. 删除排序链表中的重复元素",
"link": "/leetcode/removeDuplicatesFromSortedList"
Expand Down
1 change: 1 addition & 0 deletions docs/leetcode/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
- [61. 旋转链表](./rotateList)
- [62. 不同路径](./uniquePaths)
- [63. 不同路径 II](./uniquePathsIi)
- [64. 最小路径和](./minimumPathSum)
- [83. 删除排序链表中的重复元素](./removeDuplicatesFromSortedList)
- [2085. 统计出现过一次的公共字符串](./countCommonWordsWithOneOccurrence)
- [2182. 构造限制重复的字符串](./constructStringWithRepeatLimit)
Expand Down
12 changes: 12 additions & 0 deletions docs/leetcode/minimumPathSum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 64. 最小路径和

[64. 最小路径和](https://leetcode.cn/problems/minimum-path-sum/description/)

## 代码

::: code-group

<<< ../../src/leetcode/minimumPathSum/javascript.js{javascript} [javascript]
<<< ../../src/leetcode/minimumPathSum/typescript.ts{typescript} [typescript]

:::
24 changes: 24 additions & 0 deletions src/leetcode/minimumPathSum/javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 64. 最小路径和:https://leetcode.cn/problems/minimum-path-sum/description/
// 输入:grid = [[1,3,1],[1,5,1],[4,2,1]]
// 输出:7

export function minimumPathSum (grid) {
if (!grid.length) return 0
const row = grid.length
const col = grid[0].length

const newGrid = new Array(row).fill(0).map(() => new Array(col).fill(0))
newGrid[0][0] = grid[0][0]
for (let i = 1; i < row; i++) {
newGrid[i][0] = newGrid[i - 1][0] + grid[i][0]
}
for (let j = 1; j < col; j++) {
newGrid[0][j] = newGrid[0][j - 1] + grid[0][j]
}
for (let i = 1; i < row; i++) {
for (let j = 1; j < col; j++) {
newGrid[i][j] = Math.min(newGrid[i - 1][j] + grid[i][j], newGrid[i][j - 1] + grid[i][j])
}
}
return newGrid[row - 1][col - 1]
}
24 changes: 24 additions & 0 deletions src/leetcode/minimumPathSum/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 64. 最小路径和:https://leetcode.cn/problems/minimum-path-sum/description/
// 输入:grid = [[1,3,1],[1,5,1],[4,2,1]]
// 输出:7

export function minimumPathSum (grid: number[][]): number {
if (!grid.length) return 0
const row = grid.length
const col = grid[0].length

const newGrid: number[][] = new Array(row).fill(0).map(() => new Array(col).fill(0))
newGrid[0][0] = grid[0][0]
for (let i = 1; i < row; i++) {
newGrid[i][0] = newGrid[i - 1][0] + grid[i][0]
}
for (let j = 1; j < col; j++) {
newGrid[0][j] = newGrid[0][j - 1] + grid[0][j]
}
for (let i = 1; i < row; i++) {
for (let j = 1; j < col; j++) {
newGrid[i][j] = Math.min(newGrid[i - 1][j] + grid[i][j], newGrid[i][j - 1] + grid[i][j])
}
}
return newGrid[row - 1][col - 1]
}
13 changes: 13 additions & 0 deletions test/leetcode/minimumPathSum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect, test } from 'vitest'
import { minimumPathSum } from '../../src/leetcode/minimumPathSum/typescript.ts'
import { minimumPathSum as minimumPathSumJs } from '../../src/leetcode/minimumPathSum/javascript.js'

test(`minimumPathSum`, () => {
expect(minimumPathSum([])).toBe(0)
expect(minimumPathSum([[1,3,1],[1,5,1],[4,2,1]])).toBe(7)
})

test(`minimumPathSumJs`, () => {
expect(minimumPathSumJs([])).toBe(0)
expect(minimumPathSumJs([[1,3,1],[1,5,1],[4,2,1]])).toBe(7)
})

0 comments on commit ae3f0f3

Please sign in to comment.