Golang | Leetcode Golang题解之第64题最小路径和

题目:

题解:

Go 复制代码
func minPathSum(grid [][]int) int {
    if len(grid) == 0 || len(grid[0]) == 0 {
        return 0
    }
    rows, columns := len(grid), len(grid[0])
    dp := make([][]int, rows)
    for i := 0; i < len(dp); i++ {
        dp[i] = make([]int, columns)
    }
    dp[0][0] = grid[0][0]
    for i := 1; i < rows; i++ {
        dp[i][0] = dp[i - 1][0] + grid[i][0]
    }
    for j := 1; j < columns; j++ {
        dp[0][j] = dp[0][j - 1] + grid[0][j]
    }
    for i := 1; i < rows; i++ {
        for j := 1; j < columns; j++ {
            dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
        }
    }
    return dp[rows - 1][columns - 1]
}

func min(x, y int) int {
    if x < y {
        return x
    }
    return y
}
相关推荐
圣保罗的大教堂1 小时前
leetcode 1161. 最大层内元素和 中等
leetcode
闲看云起1 小时前
LeetCode-day6:接雨水
算法·leetcode·职场和发展
黛色正浓1 小时前
leetCode-热题100-贪心合集(JavaScript)
javascript·算法·leetcode
一起努力啊~2 小时前
算法刷题--长度最小的子数组
开发语言·数据结构·算法·leetcode
leoufung2 小时前
LeetCode 221:Maximal Square 动态规划详解
算法·leetcode·动态规划
源代码•宸2 小时前
Leetcode—39. 组合总和【中等】
经验分享·算法·leetcode·golang·sort·slices
好易学·数据结构2 小时前
可视化图解算法77:零钱兑换(兑换零钱)
数据结构·算法·leetcode·动态规划·力扣·牛客网
AlenTech3 小时前
226. 翻转二叉树 - 力扣(LeetCode)
算法·leetcode·职场和发展
Tisfy3 小时前
LeetCode 1458.两个子序列的最大点积:动态规划
算法·leetcode·动态规划·题解·dp
求梦8203 小时前
【力扣hot100题】合并区间(9)
算法·leetcode·职场和发展