You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0000-0099/0063.Unique Paths II/README_EN.md
+107-15
Original file line number
Diff line number
Diff line change
@@ -63,17 +63,17 @@ There are two ways to reach the bottom-right corner:
63
63
64
64
### Solution 1: Memoization Search
65
65
66
-
We design a function $dfs(i, j)$ to represent the number of paths from the grid $(i, j)$ to the grid $(m - 1, n - 1)$, where $m$ and $n$ are the number of rows and columns of the grid, respectively.
66
+
We design a function $\textit{dfs}(i, j)$ to represent the number of paths from the grid $(i, j)$ to the grid $(m - 1, n - 1)$. Here, $m$ and $n$ are the number of rows and columns of the grid, respectively.
67
67
68
-
The execution process of the function $dfs(i, j)$ is as follows:
68
+
The execution process of the function $\textit{dfs}(i, j)$ is as follows:
69
69
70
-
- If $i \ge m$ or $j \ge n$, or $obstacleGrid[i][j] = 1$, then the number of paths is $0$;
71
-
- If $i = m - 1$ and $j = n - 1$, then the number of paths is $1$;
72
-
- Otherwise, the number of paths is $dfs(i + 1, j) + dfs(i, j + 1)$.
70
+
- If $i \ge m$ or $j \ge n$, or $\textit{obstacleGrid}[i][j] = 1$, the number of paths is $0$;
71
+
- If $i = m - 1$ and $j = n - 1$, the number of paths is $1$;
72
+
- Otherwise, the number of paths is $\textit{dfs}(i + 1, j) + \textit{dfs}(i, j + 1)$.
73
73
74
-
To avoid repeated calculations, we can use the method of memoization search.
74
+
To avoid redundant calculations, we can use memoization.
75
75
76
-
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows and columns of the grid, respectively.
76
+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the number of rows and columns of the grid, respectively.
77
77
78
78
<!-- tabs:start -->
79
79
@@ -133,9 +133,8 @@ class Solution {
133
133
public:
134
134
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
135
135
int m = obstacleGrid.size(), n = obstacleGrid[0].size();
136
-
int f[m][n];
137
-
memset(f, -1, sizeof(f));
138
-
function<int(int, int)> dfs = [&](int i, int j) {
136
+
vector<vector<int>> f(m, vector<int>(n, -1));
137
+
auto dfs = [&](this auto&& dfs, int i, int j) {
139
138
if (i >= m || j >= n || obstacleGrid[i][j]) {
140
139
return 0;
141
140
}
@@ -204,6 +203,64 @@ function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
constf=Array.from({ length: m }, () =>Array(n).fill(-1));
248
+
constdfs= (i, j) => {
249
+
if (i >= m || j >= n || obstacleGrid[i][j] ===1) {
250
+
return0;
251
+
}
252
+
if (i === m -1&& j === n -1) {
253
+
return1;
254
+
}
255
+
if (f[i][j] ===-1) {
256
+
f[i][j] =dfs(i +1, j) +dfs(i, j +1);
257
+
}
258
+
return f[i][j];
259
+
};
260
+
returndfs(0, 0);
261
+
};
262
+
```
263
+
207
264
<!-- tabs:end -->
208
265
209
266
<!-- solution:end -->
@@ -212,16 +269,16 @@ function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
212
269
213
270
### Solution 2: Dynamic Programming
214
271
215
-
We define $f[i][j]$ as the number of paths to reach the grid $(i,j)$.
272
+
We can use a dynamic programming approach by defining a 2D array $f$, where $f[i][j]$ represents the number of paths from the grid $(0,0)$ to the grid $(i,j)$.
216
273
217
-
First, initialize all values in the first column and first row of $f$. Then, traverse other rows and columns, there are two cases:
274
+
We first initialize all values in the first column and the first row of $f$, then traverse the other rows and columns with two cases:
218
275
219
-
- If $obstacleGrid[i][j] = 1$, it means the number of paths is $0$, so $f[i][j] = 0$;
220
-
- If $obstacleGrid[i][j] = 0$, then $f[i][j] = f[i - 1][j] + f[i][j - 1]$.
276
+
- If $\textit{obstacleGrid}[i][j] = 1$, it means the number of paths is $0$, so $f[i][j] = 0$;
277
+
- If $\textit{obstacleGrid}[i][j] = 0$, then $f[i][j] = f[i - 1][j] + f[i][j - 1]$.
221
278
222
279
Finally, return $f[m - 1][n - 1]$.
223
280
224
-
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Where $m$ and $n$ are the number of rows and columns of the grid, respectively.
281
+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the number of rows and columns of the grid, respectively.
0 commit comments