Dynamic Programming Python Solution


  • 0
    E
    class Solution(object):
        def minPathSum(self, grid):
            if grid is None or len(grid) == 0:
                return 0
            else:
                row_len = len(grid)
                col_len = len(grid[0])
                
                for row in range(row_len):
                    for col in range(col_len):
                        if row == 0 and col == 0:
                            continue
                            print(grid[row][col])
                        elif row == 0:
                            grid[row][col] = grid[row][col-1] + grid[row][col]
                        elif col == 0:
                            grid[row][col] = grid[row-1][col] + grid[row][col]
                        else:
                            grid[row][col] = min(grid[row-1][col],grid[row][col-1]) + grid[row][col]
                return grid[-1][-1]
    

Log in to reply
 

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