版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412865
思路
二维的dp
dp方程是:到达每个点的时候,最小和
初始值都是0
转换方程:当前的dp值,等于之前两种走法的最小值
注意处理走到边上的情况
code
代码语言:javascript复制func minPathSum(grid [][]int) int {
m := len(grid)
if m == 0 {
return 0
}
n := len(grid[0])
for i := 0; i < m; i {
for j := 0; j < n; j {
if i == 0 && j == 0 {
continue
}
if i == 0 {
grid[i][j] = grid[i][j-1]
} else if j == 0 {
grid[i][j] = grid[i-1][j]
} else {
grid[i][j] = mymin(grid[i-1][j], grid[i][j-1])
}
}
}
return grid[m-1][n-1]
}
func mymin(x, y int) int {
if x > y {
return y
}
return x
}