62 different paths

Title: mxn a robot in a top left corner of the grid (starting point figure below labeled "Start") of the robot can only move one step down or to the right. Robot trying to reach the lower right corner of the grid (figure below labeled "Finish"). There are asked how many different paths?

Source: https://leetcode-cn.com/problems/unique-paths/

Act One: Code refer to someone else

Idea: The key is to write the state transition equation. Society Custom multidimensional list (index different) approach.

class Solution:
     DEF uniquePaths (Self, m: int, n-: int) -> int:
         # Note that by m when generating each column is the same index, 
        DP = [[. 1] * n-] * m
         for I in Range (. 1 , m):
             for J in Range (. 1 , n-): 
                DP [I] [J] = DP [I] [J -. 1] + DP [I -. 1 ] [J]
         return DP [-1 ] [-. 1 ] 

class Solution:
     DEF uniquePaths (Self, m: int, n-: int) -> int:
         # such an approach will not have the above problems, to master 
        dp = [[0] * ( n + 1 ) for iin range(m+1)]
        dp[1][0] = 1
        for i in range(1, m+1):
            for j in range(1, n+1):
                dp[i][j] = dp[i-1][j] + dp[i][j-1]
        return dp[m][n]

if __name__ == '__main__':
    duixiang = Solution()
    a = duixiang.uniquePaths(3,4)
    print(a)
View Code

ccc

Method two: mathematical methods, direct calculation method using combination.

Guess you like

Origin www.cnblogs.com/xxswkl/p/12093439.html