062. Unique Paths

Question 62

https://leetcode.com/problems/unique-paths/description/

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?

Answer

v1. Permutations (98.4%)

使用排列組合的方式做計算

1. 長、寬都要先扣除自己本身的那一格 = 步數 2. 計算 C (右步數+下步數) 取下步數

class Solution(object):
    def uniquePaths(self, m, n):
        
        # there's only way when n==1
        if n < 2:
            return 1

        # minus one for the start block
        top = m + n -2
        down = n -1
        
        # permutations
        for i in range(1,(n-1)):
            top *= (m+n-2-i)
            down *= (n-1-i)

        return top/down

Last updated