-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq62_Unique_Paths.py
75 lines (64 loc) · 1.74 KB
/
q62_Unique_Paths.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import math
class Solution:
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
if m < 2 or n < 2:
return min(m,n)
dp = {}
def path(m, n):
if (m,n) in dp:
return dp[m,n]
if m == 2 or n == 2:
dp[m,n]= max(m,n)
if m > 2 and n > 2:
dp[m,n]= path(m - 1, n) + path(m, n - 1)
return dp[m,n]
return path(m,n)
def uniquePaths1(self, m, n):
if m < 2 or n < 2:
return min(m,n)
def path(m, n):
if m == 2 or n == 2:
res[0] += max(m,n)
return
if m > 1:
path(m - 1, n)
if n > 1:
path(m, n - 1)
res = [0]
path(m, n)
return res[0]
def uniquePaths2(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
N = n + m - 2;// how much steps we need to do
k = m - 1; // number of steps that need to go down
Combination(N, k) = n! / (k!(n - k)!)
"""
return int(math.factorial(m+n-2)/(math.factorial(m-1)*math.factorial(n-1)))
def uniquePaths3(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
def comb(a, b):
stop = a - b
res = 1
while a != stop:
res *= a
a -= 1
while b != 0:
res //= b
b -= 1
return res
return comb(m + n - 2, n - 1)
for n in range(2,10):
for m in range(2,10):
print(n, m, Solution().uniquePaths1(n, m),Solution().uniquePaths(n, m))