Skip to content

Commit e4ef80e

Browse files
committed
Solution for Unique Paths problem using dynamic programming
1 parent f831fed commit e4ef80e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

unique_paths.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
2+
#
3+
# The robot can only move either down or right at any point in time.
4+
# The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
5+
#
6+
# How many possible unique paths are there?
7+
#
8+
# Note: m and n will be at most 100.
9+
"""
10+
>>> uniquePaths(3, 2)
11+
3
12+
>>> uniquePaths(7, 3)
13+
28
14+
>>> uniquePaths(0, 0)
15+
0
16+
>>> uniquePaths(0, 2)
17+
0
18+
>>> uniquePaths(2, 0)
19+
0
20+
"""
21+
22+
23+
def uniquePaths(m: int, n: int) -> int:
24+
if not m or not n:
25+
return 0
26+
dp = [[1 for col in range(n)] for row in range(m)]
27+
for i in range(1, m):
28+
for j in range(1, n):
29+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
30+
return dp[-1][-1]

0 commit comments

Comments
 (0)