-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
27 lines (23 loc) · 820 Bytes
/
answer.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
#!/usr/bin/python3
#------------------------------------------------------------------------------
class Solution:
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
# Create the grid to keep track of paths
grid = [[0]*n for _ in range(m)]
# Iterate through each path
for i in range(m):
for j in range(n):
# If start (i==0 or j==0) set to 1
if not i or not j:
grid[i][j] = 1
else:
# Sum up the possible paths reaching this spot
grid[i][j] = grid[i-1][j] + grid[i][j-1]
return grid[m-1][n-1]
#------------------------------------------------------------------------------
#Testing