-
Notifications
You must be signed in to change notification settings - Fork 43
/
unique-paths.py
249 lines (204 loc) · 6.42 KB
/
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""
62. Unique Paths
Medium
There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
The test cases are generated so that the answer will be less than or equal to 2 * 109.
Example 1:
Input: m = 3, n = 7
Output: 28
Example 2:
Input: m = 3, n = 2
Output: 3
Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Down -> Down
2. Down -> Down -> Right
3. Down -> Right -> Down
Constraints:
1 <= m, n <= 100
"""
# V0
# IDEA : BFS + dp (memory)
class Solution:
def uniquePaths(self, m, n):
# NOTE !!! we init paths as below
paths = [[1]*n for _ in range(m)]
q = deque()
q.append((0,0))
while q:
row, col = q.popleft()
if row == m or col == n or paths[row][col] > 1:
continue
if row-1 >= 0 and col-1 >= 0:
paths[row][col] = paths[row-1][col] + paths[row][col-1]
q.append((row+1, col))
q.append((row, col+1))
return paths[-1][-1]
# V0'
# IDEA : DP
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
d = [[1] * n for _ in range(m)]
for col in range(1, m):
for row in range(1, n):
d[col][row] = d[col - 1][row] + d[col][row - 1]
return d[m - 1][n - 1]
# V1
# IDEA : BFS + dp (memory)
# https://leetcode.com/problems/unique-paths/discuss/1145339/Pyhon-Beats-98-BFS
class Solution:
def uniquePaths(self, m, n):
# NOTE !!! we init paths as below
paths = [[1]*n for _ in range(m)]
q = deque()
q.append((0,0))
while q:
row, col = q.popleft()
if row == m or col == n or paths[row][col] > 1:
continue
if row-1 >= 0 and col-1 >= 0:
paths[row][col] = paths[row-1][col] + paths[row][col-1]
q.append((row+1, col))
q.append((row, col+1))
return paths[-1][-1]
# V1'
# IDEA : DP
# https://leetcode.com/problems/unique-paths/solution/
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
d = [[1] * n for _ in range(m)]
for col in range(1, m):
for row in range(1, n):
d[col][row] = d[col - 1][row] + d[col][row - 1]
return d[m - 1][n - 1]
# V1''
# IDEA : MATH
# https://leetcode.com/problems/unique-paths/solution/
from math import factorial
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
return factorial(m + n - 2) // factorial(n - 1) // factorial(m - 1)
# V1'''
# IDEA : DP
# https://leetcode.com/problems/unique-paths/discuss/162333/Python-solution
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
table = [[0]*n for _ in range(m)]
for i in range(n):
table[0][i] = 1
for j in range(m):
table[j][0] = 1
for i in range(1,m):
for j in range(1,n):
table[i][j] = table[i-1][j] + table[i][j-1]
return table[m-1][n-1]
# V1'''''
# https://blog.csdn.net/fuxuemingzhu/article/details/79337352
# IDEA : DP
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
dp = [[0] * n for _ in range(m)]
for i in range(m):
dp[i][0] = 1
for i in range(n):
dp[0][i] = 1
for i in range(1, m):
for j in range(1, n):
dp[i][j] = dp[i][j - 1] + dp[i - 1][j]
return dp[m - 1][n - 1]
# V1''''''
# https://blog.csdn.net/fuxuemingzhu/article/details/79337352
# IDEA : DP
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
dp = [[1] * n for _ in range(m)]
for i in range(m):
for j in range(n):
if i == 0 or j == 0:
continue
dp[i][j] = dp[i][j - 1] + dp[i - 1][j]
return dp[m - 1][n - 1]
# V1''''''
# https://blog.csdn.net/fuxuemingzhu/article/details/79337352
# IDEA : Permutations
class Solution(object):
def uniquePaths(self, m, n):
total = m + n - 2
v = n - 1
def permutation(m, n):
son = 1
for i in range(m, m - n, -1):
son *= i
mom = 1
for i in range(n, 0, -1):
mom *= i
return son / mom
return permutation(total, min(v, total -v))
# V1'''''''
# https://blog.csdn.net/fuxuemingzhu/article/details/79337352
# IDEA : MEMORY SEARCH
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
memo = [[0] * n for _ in range(m)]
return self.dfs(m - 1, n - 1, memo)
def dfs(self, m, n, memo):
if m == 0 or n == 0:
return 1
if memo[m][n]:
return memo[m][n]
up = self.dfs(m - 1, n, memo)
left = self.dfs(m, n - 1, memo)
memo[m][n] = up + left
return memo[m][n]
# V2
# https://blog.csdn.net/Lu_gee/article/details/76938597
# DP status equation :
# matrix[i][j] = matrix[i - 1][j] + matrix[i][j - 1]
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
matrix = [[0] * n] * m
for i in range(m):
for j in range(n):
if j == 0 or i == 0:
matrix[i][j] = 1
else:
matrix[i][j] = matrix[i - 1][j] + matrix[i][j - 1]
return matrix[m - 1][n - 1]
# V3
# Time: O(m * n)
# Space: O(m + n)
class Solution(object):
# @return an integer
def uniquePaths(self, m, n):
if m < n:
return self.uniquePaths(n, m)
ways = [1] * n
for i in range(1, m):
for j in range(1, n):
ways[j] += ways[j - 1]
return ways[n - 1]