62. Unique Paths
Given the two integers m
and n
, return the number of possible unique paths that the robot can take to go from top left to the bottom-right.
Example

Input: m = 3, n = 7 Output: 28
Note: The most beautiful DP problem.
Initializing with 1, avoids additional loops and
it's ok because we overwrite the elements anyway.
There is also a math approach (m-1 + n-1)! / (m-1)! (n-1)!
def uniquePaths(self, m: int, n: int):
dp = [[1]*n for _ in range(m)]
for r in range(1, m):
for c in range(1, n):
dp[r][c] = dp[r-1][c] + dp[r][c-1]
return dp[m-1][n-1]
Comments
Post a Comment