@@ -34,8 +34,8 @@ Output: 28
34
34
```
35
35
36
36
## 思路
37
- 这是一道典型的适合使用动态规划解决的题目,它和爬楼梯等都属于动态规划中最简单的题目,
38
- 因此也经常会被用于面试之中。
37
+
38
+ 这是一道典型的适合使用动态规划解决的题目,它和爬楼梯等都属于动态规划中最简单的题目, 因此也经常会被用于面试之中。
39
39
40
40
读完题目你就能想到动态规划的话,建立模型并解决恐怕不是难事。其实我们很容易看出,由于机器人只能右移动和下移动,
41
41
因此第[ i, j] 个格子的总数应该等于[ i - 1, j] + [ i, j -1] , 因为第[ i,j] 个格子一定是从左边或者上面移动过来的。
@@ -44,6 +44,8 @@ Output: 28
44
44
45
45
代码大概是:
46
46
47
+ JS Code:
48
+
47
49
``` js
48
50
const dp = [];
49
51
for (let i = 0 ; i < m + 1 ; i++ ) {
@@ -63,16 +65,56 @@ Output: 28
63
65
64
66
```
65
67
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
+
66
87
由于dp[ i] [ j ] 只依赖于左边的元素和上面的元素,因此空间复杂度可以进一步优化, 优化到O(n).
67
88
68
89
![ 62.unique-paths-3] ( ../assets/problems/62.unique-paths-3.png )
69
90
70
91
具体代码请查看代码区。
71
92
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
+
72
113
## 关键点
73
114
74
- - 空间复杂度可以进一步优化到O(n), 这会是一个考点
115
+ - 记忆化递归
75
116
- 基本动态规划问题
117
+ - 空间复杂度可以进一步优化到O(n), 这会是一个考点
76
118
## 代码
77
119
78
120
``` js
@@ -82,47 +124,6 @@ Output: 28
82
124
* [62] Unique Paths
83
125
*
84
126
* 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
126
127
*/
127
128
/**
128
129
* @param {number} m
@@ -141,3 +142,8 @@ var uniquePaths = function(m, n) {
141
142
return dp[n - 1 ];
142
143
};
143
144
```
145
+
146
+ ** 复杂度分析**
147
+
148
+ - 时间复杂度:$O(M * N)$
149
+ - 空间复杂度:$O(N)$
0 commit comments