Leetcode 62. Different paths (Unique Paths)

Leetcode 62. different paths

1 title description ( Leetcode topic Link )

  A robot located in a left corner mxn grid (starting point figure below labeled "Start").
  The robot can only move one step to the right or down. Robot trying to reach the bottom right corner of the grid (in the following figure labeled "Finish").
  Q. How many different paths there are in total?
Here Insert Picture Description

2 solution to a problem

  This entitled the dynamic programming problem, you can build a m n m*n of the arrays, with each record is different from the number of paths reaches that point, the initialization of the first row and first column is 1, DP state transition equation is as follows:
D P [ i ] [ j ] = D P [ i 1 ] [ j ] + D P [ i ] [ j 1 ] DP[i][j]=DP[i-1][j]+DP[i][j-1]
from the first element of the second column of a second row begins to fill DP array, the result is D P [ m 1 ] [ n 1 ] DP[m-1][n-1]

class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        DP = [([1] * (n)) for i in range(m)]
        for i in range(1,m):
            for j in range(1,n):
                DP[i][j] = DP[i-1][j]+DP[i][j-1]
        return DP[m-1][n-1]
Published 32 original articles · won praise 53 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_39378221/article/details/104028552