My python solution using Dp


  • 0
    Y

    class Solution(object):
    def minPathSum(self, grid):
    """
    :type grid: List[List[int]]
    :rtype: int
    """

        m,n = len(grid),len(grid[0])
        sum = [[0 for i in range(n)] for i in range(m)]
        
        for i in range(0,m):
            for j in range(0,n):
                if i == 0 and j == 0:
                    sum[i][j] = grid[i][j]
                elif i == 0:
                    sum[i][j] = grid[i][j] + sum[i][j-1]
                    
                elif j == 0:
                    sum[i][j] = grid[i][j] + sum[i-1][j]
                else:
                    sum[i][j] = min((sum[i-1][j]+grid[i][j]),(sum[i][j-1]+grid[i][j]))
                
        return sum[-1][-1]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.