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?
2 solution to a problem
This entitled the dynamic programming problem, you can build a
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:
from the first element of the second column of a second row begins to fill DP array, the result is
。
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]