标签:cto inf class color print unique int 规划 factor
题目描述:
方法一:
class Solution: def uniquePaths(self, m: int, n: int) -> int: return int(math.factorial(m+n-2)/math.factorial(m-1)/math.factorial(n-1))
方法二:动态规划 O(m*n) O(m*n)
class Solution: def uniquePaths(self, m: int, n: int) -> int: dp = [[1]*n] + [[1]+[0] * (n-1) for _ in range(m-1)] #print(dp) for i in range(1, m): for j in range(1, n): dp[i][j] = dp[i-1][j] + dp[i][j-1] return dp[-1][-1]
另:空间:O(n)
class Solution: def uniquePaths(self, m: int, n: int) -> int: cur = [1] * n for i in range(1, m): for j in range(1, n): cur[j] += cur[j-1] return cur[-1]
标签:cto inf class color print unique int 规划 factor
原文地址:https://www.cnblogs.com/oldby/p/11177685.html