LeetCode/2020 LeetCoding Challenge

LeetCode. Unique Paths

벤진[Benzene] 2020. 6. 30. 07:48

Problem:

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?


Above is a 7 x 3 grid. How many possible unique paths are there?

 

Example:

 

-Code-

class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        matrix = [[1 for i in range(m)] for j in range(n)]
        
        for i in range(1, n):
            for j in range(1, m):
                matrix[i][j] = matrix[i-1][j] + matrix[i][j-1]
        
        return matrix[n-1][m-1]

 

-Complexity-

Space: O(M*N)

Time:  O(M*N)

 

모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]