Minimum Path Sum

Source

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.

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

Python

class Solution:
    """
    @param grid: a list of lists of integers.
    @return: An integer, minimizes the sum of all numbers along its path
    """
    def minPathSum(self, grid):
        # write your code here
        m = len(grid)
        n = len(grid[0])

        row=[0]*m
        row[0] = grid[0][0]

        for i in range(1, m):
            row[i] = grid[i][0]+row[i-1]

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