# from math import factorial as f class Solution: # @param A : integer # @param B : integer # @return an integer hmap = {0:1, 1:1} def factorial(self, n): if n not in self.hmap: ans = n * self.factorial(n-1) self.hmap[n] = ans return self.hmap[n] # def countPaths(self, X, Y, x, y): # if x == X-1 and y == Y-1: # return 1 # elif x == X-1: # return self.countPaths(X, Y, x, y+1) # elif y == Y-1: # return self.countPaths(X, Y, x+1, y) # else: # return self.countPaths(X, Y, x+1, y) + self.countPaths(X, Y, x, y+1) # def countPaths(self, x, y): # if x == 1 or y == 1: # return 1 # else: # return self.countPaths(x-1, y) + self.countPaths(x, y-1) def uniquePaths(self, A, B): # return self.countPaths(A, B, 0, 0) #hmap = {0:1, 1:1} ##return self.countPaths(A, B) #x = min(A, B)-1 #y = max(B, A)-1 #xf = self.factorial(x) #yf = self.factorial(y) #return self.factorial(x+y)//(xf*yf) total = A + B - 2 smaller = min(A - 1, B - 1) nom = 1 den = 1 for i in range(smaller): nom *= total - i den *= i + 1 return nom//den # return self.factorial(A+B-2)/((self.factorial(A-1))*(self.factorial(B-1))) #grid = [[1 for x in range(B)] for x in range(A)] #for r in range(1, A): # for c in range(1, B): # grid[r][c] = grid[r-1][c] + grid[r][c-1] #return grid[A-1][B-1]