We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 551746a commit 6dca22bCopy full SHA for 6dca22b
unique-paths/dusunax.py
@@ -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