Python dp solution - so simple and straightforward


  • -1
    G
    class Solution(object):
    def uniquePaths(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        if (m<2 or n<2):
            return 1
        if (m==n==2):
            return 2
    
        # init
        res= [[0 for x in range(n)] for x in range(m)] 
        for i in range(m):
            res[i][0] =1
        for i in range(n):
            res[0][i]=1
    
        # calc
        for i in range(1,m):
            for j in range(1,n):
                res[i][j] = res[i-1][j]+res[i][j-1]
        
        # ret
        return res[m-1][n-1]

Log in to reply
 

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