Python code using dynamic programming

  • 0
    class Solution(object):
        def uniquePaths(self, m, n):
            :type m: int
            :type n: int
            :rtype: int
            if m==0 or n==0:    return 0
            # initialize a table with '0's, except that the first row and column are all '1's 
            table = [[0 if j!=0 else 1 for j in range(n)] if i!=0 else [1 for j in range(n)] for i in range(m)]
            # dynamic programming
            for i in xrange(1, m):
                for j in xrange(1, n):
                    table[i][j] = table[i-1][j] + table[i][j-1]
            return table[m-1][n-1]

Log in to reply

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