File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change
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 ]
You can’t perform that action at this time.
0 commit comments