DP solution in Python


  • 1
    class Solution(object):
        def uniquePaths(self, m, n):
            """
            :type m: int
            :type n: int
            :rtype: int
            """
            if m > 1 and n > 1:
                res = [[0 for p in range(n)] for q in range(m)]
                for i in range(1, m):
                    res[i][0] = 1
                for j in range(1, n):
                    res[0][j] = 1
                for i in range(1, m):
                    for j in range(1, n):
                        res[i][j] = res[i][j - 1] + res[i - 1][j]
                return res[m - 1][n - 1]
            elif m == 0 or n == 0:
                return 0
            else:
                return 1
    

Log in to reply
 

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