Python DP solution


  • 0
    M
        def minPathSum(self, grid):
            m, n = len(grid), len(grid[0])
            r = []
            for i in xrange(m):
                r.append([])
                for j in xrange(n):
                    r[i].append(grid[i][j])
                    if i and j == 0:
                        r[i][j] += r[i-1][j]
                    elif j and i == 0:
                        r[i][j] += r[i][j-1]
            for i in xrange(1, m):
                for j in xrange(1, n):
                    r[i][j] += min(r[i-1][j], r[i][j-1])
            return r[m-1][n-1]
    

Log in to reply
 

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