Skip to content

Commit 80a4854

Browse files
committed
solve: uniquePaths
1 parent c793db9 commit 80a4854

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

unique-paths/yolophg.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time Complexity: O(N * M) : iterate through a grid of size m x n once.
2+
# Space Complexity: O(N * M) : use a DP table of size (m+1) x (n+1) to store the number of paths.
3+
4+
class Solution:
5+
def uniquePaths(self, m: int, n: int) -> int:
6+
# create a dp table and add extra rows and columns for easier indexing
7+
table = [[0 for x in range(n+1)] for y in range(m+1)]
8+
9+
# set the starting point, one way to be at the start
10+
table[1][1] = 1
11+
12+
# iterate the grid to calculate paths
13+
for i in range(1, m+1):
14+
for j in range(1, n+1):
15+
# add paths going down
16+
if i+1 <= m:
17+
table[i+1][j] += table[i][j]
18+
# add paths going right
19+
if j+1 <= n:
20+
table[i][j+1] += table[i][j]
21+
22+
return table[m][n]

0 commit comments

Comments
 (0)