Skip to content

Commit cb162c8

Browse files
committed
feat: unique-paths
1 parent fc5e5d1 commit cb162c8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

dy.unique-paths.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
"""
3+
62. 不同路径
4+
https://leetcode-cn.com/problems/unique-paths/
5+
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
6+
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
7+
问总共有多少条不同的路径?
8+
"""
9+
def uniquePaths(self, m: int, n: int) -> int:
10+
# 生成缓存数组
11+
dp = [[1] * n] + [[1] + [0] * (n - 1) for _ in range(m - 1)]
12+
print(dp)
13+
for i in range(1, m):
14+
for j in range(1, n):
15+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
16+
return dp[-1][-1]
17+
18+
def uniquePathsByDy(self, m: int, n: int) -> int:
19+
if m == 1:
20+
return 1
21+
22+
if n == 1:
23+
return 1
24+
25+
return self.uniquePaths(m - 1, n) + self.uniquePaths(m, n - 1)
26+
27+
28+
so = Solution()
29+
print(so.uniquePaths(7, 3))

0 commit comments

Comments
 (0)