Unique Paths

`Medium`, `Dynamic Programming`

Question

Note: m and n will be at most 100.

``````class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
mat = [[-1 for j in range(n+1)] for i in range(m+1)]
return self.backtrack(0,0,m,n,mat)

def backtrack(self, r,c,m,n,mat):
if r == m-1 and c == n-1:
return 1
if r>=m or c >= n:
return 0
if mat[r+1][c] == -1:
mat[r+1][c] = self.backtrack(r+1,c,m,n,mat)
if mat[r][c+1] == -1:
mat[r][c+1] = self.backtrack(r,c+1,m,n,mat)
return mat[r+1][c] + mat[r][c+1]
``````

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

``````class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
import math
return math.factorial(m+n-2)/math.factorial(m-1)/math.factorial(n-1)
``````