moonlightshadow123
6/23/2017 - 7:41 AM

64. Minimum Path Sum

  1. Minimum Path Sum
class Solution(object):
    def minPathSum(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        if len(grid) == 0 or len(grid[0]) == 0:
            return 0
        rowNum = len(grid)
        colNum = len(grid[0])
        mat = [[float('inf') for _ in range(colNum+1)] for _ in range(rowNum+1)]
        mat[rowNum-1][colNum] = 0
        for row in range(rowNum-1,-1,-1):
            for col in range(colNum-1,-1,-1):
                mat[row][col] = min(mat[row+1][col], mat[row][col+1]) + grid[row][col]
        return mat[0][0]

https://leetcode.com/problems/minimum-path-sum/#/description

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.