My 60ms python solution without extra space


  • 2
    B
    class Solution(object):
    def minPathSum(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        m = len(grid)
        n = len(grid[0])
        
        if m < 2 or n < 2: return sum([sum(i) for i in grid])
    
        for i in xrange(1,m):
            grid[i][0] += grid[i-1][0]
        for i in xrange(1,n):
            grid[0][i] += grid[0][i-1]
        
        for i in xrange(1,m):
            for j in xrange(1,n):
                grid[i][j] += grid[i-1][j] if grid[i-1][j] < grid[i][j-1] else grid[i][j-1]
        
        return grid[-1][-1]

Log in to reply
 

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