leetcode 064 Minimum Path Sum 最小路径和 python 动规

版权声明:作者:onlychristmas 欢迎转载,与人分享是进步的源泉! 转载请保留原博客地址:https://blog.csdn.net/huhehaotechangsha https://blog.csdn.net/huhehaotechangsha/article/details/89147138
所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。
'''
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

Example:

Input:
[
  [1,3,1],
  [1,5,1],
  [4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.
'''


class Solution:
    def minPathSum(self, grid: List[List[int]]) -> int:

        # Approach one
        n , m = len(grid), len(grid[0])
        for i in range(1,m): grid[0][i] += grid[0][i-1]
        for i in range(1,n): grid[i][0] += grid[i-1][0]
        for i in range(1,n):
            for j in range(1,m):
                grid[i][j] += min(grid[i-1][j],grid[i][j-1])
        return grid[n-1][m-1]

        

所有Leetcode题目不定期汇总在 Github, 欢迎大家批评指正,讨论交流。

猜你喜欢

转载自blog.csdn.net/huhehaotechangsha/article/details/89147138