View difference between Paste ID: 7hbFPTgp and wvVm1NuA
SHOW: | | - or go back to the newest paste.
1
# from math import factorial as f
2
class Solution:
3
	# @param A : integer
4
	# @param B : integer
5
	# @return an integer
6
	
7
	hmap = {0:1, 1:1}
8
	def factorial(self, n):
9
        if n not in self.hmap:
10
            ans = n * self.factorial(n-1)
11
            self.hmap[n] = ans
12
        return self.hmap[n]
13
14
    # def countPaths(self, X, Y, x, y):
15
    #     if x == X-1 and y == Y-1:
16
    #         return 1
17
    #     elif x == X-1:
18
    #         return self.countPaths(X, Y, x, y+1)
19
    #     elif y == Y-1:
20
    #         return self.countPaths(X, Y, x+1, y)
21
    #     else:
22
    #         return self.countPaths(X, Y, x+1, y) + self.countPaths(X, Y, x, y+1)
23
    
24
    # def countPaths(self, x, y):
25
    #     if x == 1 or y == 1:
26
    #         return 1
27
    #     else:
28
    #         return self.countPaths(x-1, y) + self.countPaths(x, y-1)
29
            
30
	
31
	def uniquePaths(self, A, B):
32
	   # return self.countPaths(A, B, 0, 0)
33
	   #hmap = {0:1, 1:1}
34
	   ##return self.countPaths(A, B)
35
	   #x = min(A, B)-1
36
	   #y = max(B, A)-1
37
	   #xf = self.factorial(x)
38
	   #yf = self.factorial(y)
39
	   #return self.factorial(x+y)//(xf*yf)
40
	   
41
        total = A + B - 2
42
        smaller = min(A - 1, B - 1)
43
        nom = 1
44
        den = 1
45
        for i in range(smaller):
46
            nom *= total - i
47
            den *= i + 1
48
        return nom//den
49
	   
50
	   # return self.factorial(A+B-2)/((self.factorial(A-1))*(self.factorial(B-1)))
51
	   
52
	   #grid = [[1 for x in range(B)] for x in range(A)]
53
	   #for r in range(1, A):
54
	   #    for c in range(1, B):
55
	   #        grid[r][c] = grid[r-1][c] + grid[r][c-1]
56
	   #return grid[A-1][B-1]
57
	   
58
	   
59