Day38力扣打卡

打卡记录

Day38力扣打卡_第1张图片

网格中的最小路径代价(动态规划)

链接

class Solution:
    def minPathCost(self, grid: List[List[int]], moveCost: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])
        f = [[0x3f3f3f3f3f] * n for _ in range(m)]
        f[0] = grid[0]
        for i in range(1, m):
            for j in range(n):
                for k in range(n):
                    idx = grid[i - 1][k]
                    f[i][j] = min(f[i][j], f[i - 1][k] + moveCost[idx][j] + grid[i][j])
        return min(f[-1])

你可能感兴趣的:(leetcode刷题打卡,leetcode,算法,python)