Skip to content

Commit 6dca22b

Browse files
committed
add solution: unique-paths
1 parent 551746a commit 6dca22b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

unique-paths/dusunax.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
# 62. Unique Paths
3+
4+
use dynamic programming & a dp table to store the number of ways to reach each cell.
5+
6+
### TC is O(m * n):
7+
- iterating through every cell in the grid. = O(m * n)
8+
- updating each cell in the grid. = O(1)
9+
10+
### SC is O(m * n):
11+
- using a dp table (2D array) to store the number of ways to reach each cell. = O(m * n)
12+
'''
13+
class Solution:
14+
def uniquePaths(self, m: int, n: int) -> int:
15+
dp = [[1] * n for _ in range(m)]
16+
17+
for y in range(1, m):
18+
for x in range(1, n):
19+
dp[y][x] = dp[y - 1][x] + dp[y][x - 1]
20+
21+
return dp[m - 1][n - 1]

0 commit comments

Comments
 (0)