본문 바로가기
LeetCode/2020 LeetCoding Challenge

LeetCode. Unique Paths

by 벤진[Benzene] 2020. 6. 30.

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]

'LeetCode > 2020 LeetCoding Challenge' 카테고리의 다른 글

LeetCode. Arranging Coins  (0) 2020.07.02
LeetCode. Sum Root to Leaf Numbers  (0) 2020.06.27
LeetCode. Count Complete Tree Nodes  (0) 2020.06.24
LeetCode.Single Number II  (0) 2020.06.23
LeetCode.H-Index II  (0) 2020.06.19

댓글