diff --git a/README_EN.md b/README_EN.md index d81be5f48286a..47ba7fdfb46b0 100644 --- a/README_EN.md +++ b/README_EN.md @@ -18,9 +18,9 @@ This project contains solutions for problems from LeetCode, "Coding Interviews ( [中文文档](/README.md) -## Sites +## Site -https://doocs.github.io/leetcode +https://doocs.github.io/leetcode/en ## Solutions @@ -31,8 +31,8 @@ https://doocs.github.io/leetcode ## JavaScript & Database Practice -- [JavaScript Practice](/solution/JAVASCRIPT_README_EN.md) -- [Database Practice](/solution/DATABASE_README_EN.md) +- [JavaScript](/solution/JAVASCRIPT_README_EN.md) +- [Database](/solution/DATABASE_README_EN.md) ## Topics diff --git a/solution/0000-0099/0063.Unique Paths II/README.md b/solution/0000-0099/0063.Unique Paths II/README.md index f770bea10abac..4b10cca3b87e0 100644 --- a/solution/0000-0099/0063.Unique Paths II/README.md +++ b/solution/0000-0099/0063.Unique Paths II/README.md @@ -65,13 +65,13 @@ tags: ### 方法一:记忆化搜索 -我们设计一个函数 $dfs(i, j)$ 表示从网格 $(i, j)$ 到网格 $(m - 1, n - 1)$ 的路径数。其中 $m$ 和 $n$ 分别是网格的行数和列数。 +我们设计一个函数 $\textit{dfs}(i, j)$ 表示从网格 $(i, j)$ 到网格 $(m - 1, n - 1)$ 的路径数。其中 $m$ 和 $n$ 分别是网格的行数和列数。 -函数 $dfs(i, j)$ 的执行过程如下: +函数 $\textit{dfs}(i, j)$ 的执行过程如下: -- 如果 $i \ge m$ 或者 $j \ge n$,或者 $obstacleGrid[i][j] = 1$,则路径数为 $0$; +- 如果 $i \ge m$ 或者 $j \ge n$,或者 $\textit{obstacleGrid}[i][j] = 1$,则路径数为 $0$; - 如果 $i = m - 1$ 且 $j = n - 1$,则路径数为 $1$; -- 否则,路径数为 $dfs(i + 1, j) + dfs(i, j + 1)$。 +- 否则,路径数为 $\textit{dfs}(i + 1, j) + \textit{dfs}(i, j + 1)$。 为了避免重复计算,我们可以使用记忆化搜索的方法。 @@ -135,9 +135,8 @@ class Solution { public: int uniquePathsWithObstacles(vector>& obstacleGrid) { int m = obstacleGrid.size(), n = obstacleGrid[0].size(); - int f[m][n]; - memset(f, -1, sizeof(f)); - function dfs = [&](int i, int j) { + vector> f(m, vector(n, -1)); + auto dfs = [&](this auto&& dfs, int i, int j) { if (i >= m || j >= n || obstacleGrid[i][j]) { return 0; } @@ -206,6 +205,64 @@ function uniquePathsWithObstacles(obstacleGrid: number[][]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn unique_paths_with_obstacles(obstacle_grid: Vec>) -> i32 { + let m = obstacle_grid.len(); + let n = obstacle_grid[0].len(); + let mut f = vec![vec![-1; n]; m]; + Self::dfs(0, 0, &obstacle_grid, &mut f) + } + + fn dfs(i: usize, j: usize, obstacle_grid: &Vec>, f: &mut Vec>) -> i32 { + let m = obstacle_grid.len(); + let n = obstacle_grid[0].len(); + if i >= m || j >= n || obstacle_grid[i][j] == 1 { + return 0; + } + if i == m - 1 && j == n - 1 { + return 1; + } + if f[i][j] != -1 { + return f[i][j]; + } + let down = Self::dfs(i + 1, j, obstacle_grid, f); + let right = Self::dfs(i, j + 1, obstacle_grid, f); + f[i][j] = down + right; + f[i][j] + } +} +``` + +#### JavaScript + +```js +/** + * @param {number[][]} obstacleGrid + * @return {number} + */ +var uniquePathsWithObstacles = function (obstacleGrid) { + const m = obstacleGrid.length; + const n = obstacleGrid[0].length; + const f = Array.from({ length: m }, () => Array(n).fill(-1)); + const dfs = (i, j) => { + if (i >= m || j >= n || obstacleGrid[i][j] === 1) { + return 0; + } + if (i === m - 1 && j === n - 1) { + return 1; + } + if (f[i][j] === -1) { + f[i][j] = dfs(i + 1, j) + dfs(i, j + 1); + } + return f[i][j]; + }; + return dfs(0, 0); +}; +``` + @@ -214,12 +271,12 @@ function uniquePathsWithObstacles(obstacleGrid: number[][]): number { ### 方法二:动态规划 -我们定义 $f[i][j]$ 表示到达网格 $(i,j)$ 的路径数。 +我们可以使用动态规划的方法,定义一个二维数组 $f$,其中 $f[i][j]$ 表示从网格 $(0,0)$ 到网格 $(i,j)$ 的路径数。 -首先初始化 $f$ 第一列和第一行的所有值,然后遍历其它行和列,有两种情况: +我们首先初始化 $f$ 的第一列和第一行的所有值,然后遍历其它行和列,有两种情况: -- 若 $obstacleGrid[i][j] = 1$,说明路径数为 $0$,那么 $f[i][j] = 0$; -- 若 $obstacleGrid[i][j] = 0$,则 $f[i][j] = f[i - 1][j] + f[i][j - 1]$。 +- 若 $\textit{obstacleGrid}[i][j] = 1$,说明路径数为 $0$,那么 $f[i][j] = 0$; +- 若 $\textit{obstacleGrid}[i][j] = 0$,则 $f[i][j] = f[i - 1][j] + f[i][j - 1]$。 最后返回 $f[m - 1][n - 1]$ 即可。 @@ -357,29 +414,6 @@ function uniquePathsWithObstacles(obstacleGrid: number[][]): number { } ``` -#### TypeScript - -```ts -function uniquePathsWithObstacles(obstacleGrid: number[][]): number { - const m = obstacleGrid.length; - const n = obstacleGrid[0].length; - const f: number[][] = Array.from({ length: m }, () => Array(n).fill(-1)); - const dfs = (i: number, j: number): number => { - if (i >= m || j >= n || obstacleGrid[i][j] === 1) { - return 0; - } - if (i === m - 1 && j === n - 1) { - return 1; - } - if (f[i][j] === -1) { - f[i][j] = dfs(i + 1, j) + dfs(i, j + 1); - } - return f[i][j]; - }; - return dfs(0, 0); -} -``` - #### Rust ```rust @@ -413,6 +447,41 @@ impl Solution { } ``` +#### JavaScript + +```js +/** + * @param {number[][]} obstacleGrid + * @return {number} + */ +var uniquePathsWithObstacles = function (obstacleGrid) { + const m = obstacleGrid.length; + const n = obstacleGrid[0].length; + const f = Array.from({ length: m }, () => Array(n).fill(0)); + for (let i = 0; i < m; i++) { + if (obstacleGrid[i][0] === 1) { + break; + } + f[i][0] = 1; + } + for (let i = 0; i < n; i++) { + if (obstacleGrid[0][i] === 1) { + break; + } + f[0][i] = 1; + } + for (let i = 1; i < m; i++) { + for (let j = 1; j < n; j++) { + if (obstacleGrid[i][j] === 1) { + continue; + } + f[i][j] = f[i - 1][j] + f[i][j - 1]; + } + } + return f[m - 1][n - 1]; +}; +``` + diff --git a/solution/0000-0099/0063.Unique Paths II/README_EN.md b/solution/0000-0099/0063.Unique Paths II/README_EN.md index 9ea8e7ff71466..0ad42812f6587 100644 --- a/solution/0000-0099/0063.Unique Paths II/README_EN.md +++ b/solution/0000-0099/0063.Unique Paths II/README_EN.md @@ -63,17 +63,17 @@ There are two ways to reach the bottom-right corner: ### Solution 1: Memoization Search -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. +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. -The execution process of the function $dfs(i, j)$ is as follows: +The execution process of the function $\textit{dfs}(i, j)$ is as follows: -- If $i \ge m$ or $j \ge n$, or $obstacleGrid[i][j] = 1$, then the number of paths is $0$; -- If $i = m - 1$ and $j = n - 1$, then the number of paths is $1$; -- Otherwise, the number of paths is $dfs(i + 1, j) + dfs(i, j + 1)$. +- If $i \ge m$ or $j \ge n$, or $\textit{obstacleGrid}[i][j] = 1$, the number of paths is $0$; +- If $i = m - 1$ and $j = n - 1$, the number of paths is $1$; +- Otherwise, the number of paths is $\textit{dfs}(i + 1, j) + \textit{dfs}(i, j + 1)$. -To avoid repeated calculations, we can use the method of memoization search. +To avoid redundant calculations, we can use memoization. -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. +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. @@ -133,9 +133,8 @@ class Solution { public: int uniquePathsWithObstacles(vector>& obstacleGrid) { int m = obstacleGrid.size(), n = obstacleGrid[0].size(); - int f[m][n]; - memset(f, -1, sizeof(f)); - function dfs = [&](int i, int j) { + vector> f(m, vector(n, -1)); + auto dfs = [&](this auto&& dfs, int i, int j) { if (i >= m || j >= n || obstacleGrid[i][j]) { return 0; } @@ -204,6 +203,64 @@ function uniquePathsWithObstacles(obstacleGrid: number[][]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn unique_paths_with_obstacles(obstacle_grid: Vec>) -> i32 { + let m = obstacle_grid.len(); + let n = obstacle_grid[0].len(); + let mut f = vec![vec![-1; n]; m]; + Self::dfs(0, 0, &obstacle_grid, &mut f) + } + + fn dfs(i: usize, j: usize, obstacle_grid: &Vec>, f: &mut Vec>) -> i32 { + let m = obstacle_grid.len(); + let n = obstacle_grid[0].len(); + if i >= m || j >= n || obstacle_grid[i][j] == 1 { + return 0; + } + if i == m - 1 && j == n - 1 { + return 1; + } + if f[i][j] != -1 { + return f[i][j]; + } + let down = Self::dfs(i + 1, j, obstacle_grid, f); + let right = Self::dfs(i, j + 1, obstacle_grid, f); + f[i][j] = down + right; + f[i][j] + } +} +``` + +#### JavaScript + +```js +/** + * @param {number[][]} obstacleGrid + * @return {number} + */ +var uniquePathsWithObstacles = function (obstacleGrid) { + const m = obstacleGrid.length; + const n = obstacleGrid[0].length; + const f = Array.from({ length: m }, () => Array(n).fill(-1)); + const dfs = (i, j) => { + if (i >= m || j >= n || obstacleGrid[i][j] === 1) { + return 0; + } + if (i === m - 1 && j === n - 1) { + return 1; + } + if (f[i][j] === -1) { + f[i][j] = dfs(i + 1, j) + dfs(i, j + 1); + } + return f[i][j]; + }; + return dfs(0, 0); +}; +``` + @@ -212,16 +269,16 @@ function uniquePathsWithObstacles(obstacleGrid: number[][]): number { ### Solution 2: Dynamic Programming -We define $f[i][j]$ as the number of paths to reach the grid $(i,j)$. +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)$. -First, initialize all values in the first column and first row of $f$. Then, traverse other rows and columns, there are two cases: +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: -- If $obstacleGrid[i][j] = 1$, it means the number of paths is $0$, so $f[i][j] = 0$; -- If $obstacleGrid[i][j] = 0$, then $f[i][j] = f[i - 1][j] + f[i][j - 1]$. +- If $\textit{obstacleGrid}[i][j] = 1$, it means the number of paths is $0$, so $f[i][j] = 0$; +- If $\textit{obstacleGrid}[i][j] = 0$, then $f[i][j] = f[i - 1][j] + f[i][j - 1]$. Finally, return $f[m - 1][n - 1]$. -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. +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. @@ -388,6 +445,41 @@ impl Solution { } ``` +#### JavaScript + +```js +/** + * @param {number[][]} obstacleGrid + * @return {number} + */ +var uniquePathsWithObstacles = function (obstacleGrid) { + const m = obstacleGrid.length; + const n = obstacleGrid[0].length; + const f = Array.from({ length: m }, () => Array(n).fill(0)); + for (let i = 0; i < m; i++) { + if (obstacleGrid[i][0] === 1) { + break; + } + f[i][0] = 1; + } + for (let i = 0; i < n; i++) { + if (obstacleGrid[0][i] === 1) { + break; + } + f[0][i] = 1; + } + for (let i = 1; i < m; i++) { + for (let j = 1; j < n; j++) { + if (obstacleGrid[i][j] === 1) { + continue; + } + f[i][j] = f[i - 1][j] + f[i][j - 1]; + } + } + return f[m - 1][n - 1]; +}; +``` + diff --git a/solution/0000-0099/0063.Unique Paths II/Solution.cpp b/solution/0000-0099/0063.Unique Paths II/Solution.cpp index b363c7f95535d..44a8ae85688df 100644 --- a/solution/0000-0099/0063.Unique Paths II/Solution.cpp +++ b/solution/0000-0099/0063.Unique Paths II/Solution.cpp @@ -2,9 +2,8 @@ class Solution { public: int uniquePathsWithObstacles(vector>& obstacleGrid) { int m = obstacleGrid.size(), n = obstacleGrid[0].size(); - int f[m][n]; - memset(f, -1, sizeof(f)); - function dfs = [&](int i, int j) { + vector> f(m, vector(n, -1)); + auto dfs = [&](this auto&& dfs, int i, int j) { if (i >= m || j >= n || obstacleGrid[i][j]) { return 0; } @@ -18,4 +17,4 @@ class Solution { }; return dfs(0, 0); } -}; \ No newline at end of file +}; diff --git a/solution/0000-0099/0063.Unique Paths II/Solution.js b/solution/0000-0099/0063.Unique Paths II/Solution.js new file mode 100644 index 0000000000000..ee92b5eb809c5 --- /dev/null +++ b/solution/0000-0099/0063.Unique Paths II/Solution.js @@ -0,0 +1,22 @@ +/** + * @param {number[][]} obstacleGrid + * @return {number} + */ +var uniquePathsWithObstacles = function (obstacleGrid) { + const m = obstacleGrid.length; + const n = obstacleGrid[0].length; + const f = Array.from({ length: m }, () => Array(n).fill(-1)); + const dfs = (i, j) => { + if (i >= m || j >= n || obstacleGrid[i][j] === 1) { + return 0; + } + if (i === m - 1 && j === n - 1) { + return 1; + } + if (f[i][j] === -1) { + f[i][j] = dfs(i + 1, j) + dfs(i, j + 1); + } + return f[i][j]; + }; + return dfs(0, 0); +}; diff --git a/solution/0000-0099/0063.Unique Paths II/Solution.rs b/solution/0000-0099/0063.Unique Paths II/Solution.rs new file mode 100644 index 0000000000000..770d83e0f78e9 --- /dev/null +++ b/solution/0000-0099/0063.Unique Paths II/Solution.rs @@ -0,0 +1,26 @@ +impl Solution { + pub fn unique_paths_with_obstacles(obstacle_grid: Vec>) -> i32 { + let m = obstacle_grid.len(); + let n = obstacle_grid[0].len(); + let mut f = vec![vec![-1; n]; m]; + Self::dfs(0, 0, &obstacle_grid, &mut f) + } + + fn dfs(i: usize, j: usize, obstacle_grid: &Vec>, f: &mut Vec>) -> i32 { + let m = obstacle_grid.len(); + let n = obstacle_grid[0].len(); + if i >= m || j >= n || obstacle_grid[i][j] == 1 { + return 0; + } + if i == m - 1 && j == n - 1 { + return 1; + } + if f[i][j] != -1 { + return f[i][j]; + } + let down = Self::dfs(i + 1, j, obstacle_grid, f); + let right = Self::dfs(i, j + 1, obstacle_grid, f); + f[i][j] = down + right; + f[i][j] + } +} diff --git a/solution/0000-0099/0063.Unique Paths II/Solution2.js b/solution/0000-0099/0063.Unique Paths II/Solution2.js new file mode 100644 index 0000000000000..818efb306de28 --- /dev/null +++ b/solution/0000-0099/0063.Unique Paths II/Solution2.js @@ -0,0 +1,30 @@ +/** + * @param {number[][]} obstacleGrid + * @return {number} + */ +var uniquePathsWithObstacles = function (obstacleGrid) { + const m = obstacleGrid.length; + const n = obstacleGrid[0].length; + const f = Array.from({ length: m }, () => Array(n).fill(0)); + for (let i = 0; i < m; i++) { + if (obstacleGrid[i][0] === 1) { + break; + } + f[i][0] = 1; + } + for (let i = 0; i < n; i++) { + if (obstacleGrid[0][i] === 1) { + break; + } + f[0][i] = 1; + } + for (let i = 1; i < m; i++) { + for (let j = 1; j < n; j++) { + if (obstacleGrid[i][j] === 1) { + continue; + } + f[i][j] = f[i - 1][j] + f[i][j - 1]; + } + } + return f[m - 1][n - 1]; +}; diff --git a/solution/0000-0099/0063.Unique Paths II/images/robot_maze.png b/solution/0000-0099/0063.Unique Paths II/images/robot_maze.png deleted file mode 100644 index e1aa28362c8cf..0000000000000 Binary files a/solution/0000-0099/0063.Unique Paths II/images/robot_maze.png and /dev/null differ