Leetcode - 62 different paths python

Disclaimer: This article is original, All Rights Reserved https://blog.csdn.net/weixin_41864878/article/details/90520351

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?

Description: the values ​​of m and n is not more than 100.

Example 1:

Input: m = 3, n = 2 Output: 3 Explanation: From top left corner, a total of three paths to the lower right corner.

  1. Right -> Right -> Down
  2. Right -> Down -> right
  3. Down -> right -> right Example 2:

Input: m = 7, n = 3 Output: 28

This problem is relatively simple equation is the step [i] [j] = step [i-1] [j] + step [i] [j-1]
and then note the boundary conditions can be

class Solution(object):
    def uniquePaths(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        if not m and not n: return 0
        if not m or not n: return 1
        if m == n == 1: return 1
        step = [[1 for i in range(m)] for j in range(n)]
        step[0][0] = 0
        for i in range(1, n):
            for j in range(1, m):
                step[i][j] = step[i-1][j] + step[i][j-1]
        return step[n-1][m-1]

Guess you like

Origin blog.csdn.net/weixin_41864878/article/details/90520351