Skip to content

Commit d71918e

Browse files
authored
Update 62.unique-paths.md
1 parent 98d0e91 commit d71918e

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

problems/62.unique-paths.md

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ Output: 28
3434
```
3535

3636
## 思路
37-
这是一道典型的适合使用动态规划解决的题目,它和爬楼梯等都属于动态规划中最简单的题目,
38-
因此也经常会被用于面试之中。
37+
38+
这是一道典型的适合使用动态规划解决的题目,它和爬楼梯等都属于动态规划中最简单的题目,因此也经常会被用于面试之中。
3939

4040
读完题目你就能想到动态规划的话,建立模型并解决恐怕不是难事。其实我们很容易看出,由于机器人只能右移动和下移动,
4141
因此第[i, j]个格子的总数应该等于[i - 1, j] + [i, j -1], 因为第[i,j]个格子一定是从左边或者上面移动过来的。
@@ -44,6 +44,8 @@ Output: 28
4444

4545
代码大概是:
4646

47+
JS Code:
48+
4749
```js
4850
const dp = [];
4951
for (let i = 0; i < m + 1; i++) {
@@ -63,16 +65,56 @@ Output: 28
6365

6466
```
6567

68+
Python Code:
69+
70+
```python
71+
class Solution:
72+
def uniquePaths(self, m: int, n: int) -> int:
73+
d = [[1] * n for _ in range(m)]
74+
75+
for col in range(1, m):
76+
for row in range(1, n):
77+
d[col][row] = d[col - 1][row] + d[col][row - 1]
78+
79+
return d[m - 1][n - 1]
80+
```
81+
82+
**复杂度分析**
83+
84+
- 时间复杂度:$O(M * N)$
85+
- 空间复杂度:$O(M * N)$
86+
6687
由于dp[i][j] 只依赖于左边的元素和上面的元素,因此空间复杂度可以进一步优化, 优化到O(n).
6788

6889
![62.unique-paths-3](../assets/problems/62.unique-paths-3.png)
6990

7091
具体代码请查看代码区。
7192

93+
94+
当然你也可以使用记忆化递归的方式来进行,由于递归深度的原因,性能比上面的方法差不少:
95+
96+
> 直接暴力递归的话会超时。
97+
98+
Python3 Code:
99+
```python
100+
class Solution:
101+
visited = dict()
102+
103+
def uniquePaths(self, m: int, n: int) -> int:
104+
if (m, n) in self.visited:
105+
return self.visited[(m, n)]
106+
if m == 1 or n == 1:
107+
return 1
108+
cnt = self.uniquePaths(m - 1, n) + self.uniquePaths(m, n - 1)
109+
self.visited[(m, n)] = cnt
110+
return cnt
111+
```
112+
72113
## 关键点
73114

74-
- 空间复杂度可以进一步优化到O(n), 这会是一个考点
115+
- 记忆化递归
75116
- 基本动态规划问题
117+
- 空间复杂度可以进一步优化到O(n), 这会是一个考点
76118
## 代码
77119

78120
```js
@@ -82,47 +124,6 @@ Output: 28
82124
* [62] Unique Paths
83125
*
84126
* https://leetcode.com/problems/unique-paths/description/
85-
*
86-
* algorithms
87-
* Medium (46.53%)
88-
* Total Accepted: 277K
89-
* Total Submissions: 587.7K
90-
* Testcase Example: '3\n2'
91-
*
92-
* A robot is located at the top-left corner of a m x n grid (marked 'Start' in
93-
* the diagram below).
94-
*
95-
* The robot can only move either down or right at any point in time. The robot
96-
* is trying to reach the bottom-right corner of the grid (marked 'Finish' in
97-
* the diagram below).
98-
*
99-
* How many possible unique paths are there?
100-
*
101-
*
102-
* Above is a 7 x 3 grid. How many possible unique paths are there?
103-
*
104-
* Note: m and n will be at most 100.
105-
*
106-
* Example 1:
107-
*
108-
*
109-
* Input: m = 3, n = 2
110-
* Output: 3
111-
* Explanation:
112-
* From the top-left corner, there are a total of 3 ways to reach the
113-
* bottom-right corner:
114-
* 1. Right -> Right -> Down
115-
* 2. Right -> Down -> Right
116-
* 3. Down -> Right -> Right
117-
*
118-
*
119-
* Example 2:
120-
*
121-
*
122-
* Input: m = 7, n = 3
123-
* Output: 28
124-
*
125-
* START
126127
*/
127128
/**
128129
* @param {number} m
@@ -141,3 +142,8 @@ var uniquePaths = function(m, n) {
141142
return dp[n - 1];
142143
};
143144
```
145+
146+
**复杂度分析**
147+
148+
- 时间复杂度:$O(M * N)$
149+
- 空间复杂度:$O(N)$

0 commit comments

Comments
 (0)