Dp, clean python code


  • 0
    class Solution(object):
        '''dp[i][j] represents the minimizes sum from (0, 0) to (i, j)'''
    
        def minPathSum(self, grid):
            m, n = len(grid), len(grid[0])
    
            dp = [[0] * n for _ in xrange(m)]
            for i in xrange(m):
                for j in xrange(n):
                    dp[i][j] = (0 if not i + j else min(dp[i][j - 1] if j > 0 else float('inf'), dp[i - 1][j] if i > 0 else float('inf'))) + grid[i][j]
            return dp[m - 1][n - 1]

Log in to reply
 

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