From 0e0c06744e550c1a0658a042052e34e1e7de008a Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 15 Feb 2025 08:12:33 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1706 No.1706.Where Will the Ball Fall --- .../1706.Where Will the Ball Fall/README.md | 46 +++++++++++---- .../README_EN.md | 58 ++++++++++++++----- .../1706.Where Will the Ball Fall/Solution.js | 25 ++++++++ .../1706.Where Will the Ball Fall/Solution.rs | 6 +- .../1706.Where Will the Ball Fall/Solution.ts | 6 +- 5 files changed, 107 insertions(+), 34 deletions(-) create mode 100644 solution/1700-1799/1706.Where Will the Ball Fall/Solution.js diff --git a/solution/1700-1799/1706.Where Will the Ball Fall/README.md b/solution/1700-1799/1706.Where Will the Ball Fall/README.md index 5b28a84722165..7e2f6bf2d9fae 100644 --- a/solution/1700-1799/1706.Where Will the Ball Fall/README.md +++ b/solution/1700-1799/1706.Where Will the Ball Fall/README.md @@ -84,7 +84,7 @@ b4 球开始放在第 4 列上,会卡在第 2、3 列和第 1 行之间的 "V" ### 方法一:分情况讨论 + DFS -我们可以使用 DFS 来模拟球的运动过程,设计一个函数 $dfs(i, j)$,表示球从第 $i$ 行第 $j$ 列出发,最终会落在第几列。对于以下情况,球会卡住: +我们可以使用 DFS 来模拟球的运动过程,设计一个函数 $\textit{dfs}(i, j)$,表示球从第 $i$ 行第 $j$ 列出发,最终会落在第几列。对于以下情况,球会卡住: 1. 球位于最左一列,并且球所在的单元格单元格挡板将球导向左侧 1. 球位于最右一列,并且此单元格挡板将球导向右侧 @@ -93,7 +93,7 @@ b4 球开始放在第 4 列上,会卡在第 2、3 列和第 1 行之间的 "V" 如果满足以上任意一种情况,我们就可以判断球会卡住,返回 $-1$。否则,我们就可以继续递归地寻找球的下一个位置。最后,如果球到了最后一行,我们就可以返回当前列的编号。 -时间复杂度 $O(m \times n)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别是数组 $grid$ 的行数和列数。 +时间复杂度 $O(m \times n)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别是数组 $\textit{grid}$ 的行数和列数。 @@ -249,11 +249,7 @@ function findBall(grid: number[][]): number[] { return dfs(i + 1, j - 1); } }; - const ans: number[] = []; - for (let j = 0; j < n; ++j) { - ans.push(dfs(0, j)); - } - return ans; + return Array.from({ length: n }, (_, j) => dfs(0, j)); } ``` @@ -281,15 +277,45 @@ impl Solution { pub fn find_ball(grid: Vec>) -> Vec { let m = grid.len(); let n = grid[0].len(); - let mut res = vec![0; n]; + let mut ans = vec![0; n]; for i in 0..n { - res[i] = Self::dfs(&grid, 0, i); + ans[i] = Self::dfs(&grid, 0, i); } - res + ans } } ``` +#### JavaScript + +```js +/** + * @param {number[][]} grid + * @return {number[]} + */ +var findBall = function (grid) { + const m = grid.length; + const n = grid[0].length; + const dfs = (i, j) => { + if (i === m) { + return j; + } + if (grid[i][j] === 1) { + if (j === n - 1 || grid[i][j + 1] === -1) { + return -1; + } + return dfs(i + 1, j + 1); + } else { + if (j === 0 || grid[i][j - 1] === 1) { + return -1; + } + return dfs(i + 1, j - 1); + } + }; + return Array.from({ length: n }, (_, j) => dfs(0, j)); +}; +``` + diff --git a/solution/1700-1799/1706.Where Will the Ball Fall/README_EN.md b/solution/1700-1799/1706.Where Will the Ball Fall/README_EN.md index e0ac840dd1195..89c141f0cd0cf 100644 --- a/solution/1700-1799/1706.Where Will the Ball Fall/README_EN.md +++ b/solution/1700-1799/1706.Where Will the Ball Fall/README_EN.md @@ -80,18 +80,18 @@ Ball b4 is dropped at column 4 and will get stuck on the box between column 2 an -### Solution 1: Case Discussion + DFS +### Solution 1: Case Analysis + DFS -We can use DFS to simulate the movement of the ball. We design a function $dfs(i, j)$, which represents that the ball starts from the $i$th row and the $j$th column, and finally falls in which column. The ball will get stuck in the following situations: +We can use DFS to simulate the movement of the ball. Design a function $\textit{dfs}(i, j)$, which represents the column where the ball will fall when it starts from row $i$ and column $j$. The ball will get stuck in the following cases: -1. The ball is in the leftmost column, and the cell's vane directs the ball to the left. -2. The ball is in the rightmost column, and the cell's vane directs the ball to the right. -3. The cell's vane where the ball is located directs the ball to the right, and the vane of the cell adjacent to the right of the ball directs the ball to the left. -4. The cell's vane where the ball is located directs the ball to the left, and the vane of the cell adjacent to the left of the ball directs the ball to the right. +1. The ball is in the leftmost column, and the cell's diagonal directs the ball to the left. +2. The ball is in the rightmost column, and the cell's diagonal directs the ball to the right. +3. The cell's diagonal directs the ball to the right, and the adjacent cell to the right directs the ball to the left. +4. The cell's diagonal directs the ball to the left, and the adjacent cell to the left directs the ball to the right. -If any of the above situations are met, we can judge that the ball will get stuck and return $-1$. Otherwise, we can continue to recursively find the next position of the ball. Finally, if the ball reaches the last row, we can return the current column number. +If any of the above conditions are met, we can determine that the ball will get stuck and return $-1$. Otherwise, we can continue to recursively find the next position of the ball. Finally, if the ball reaches the last row, we can return the current column index. -The time complexity is $O(m \times n)$, and the space complexity is $O(m)$. Where $m$ and $n$ are the number of rows and columns of the array $grid$, respectively. +The time complexity is $O(m \times n)$, and the space complexity is $O(m)$. Here, $m$ and $n$ are the number of rows and columns of the grid, respectively. @@ -247,11 +247,7 @@ function findBall(grid: number[][]): number[] { return dfs(i + 1, j - 1); } }; - const ans: number[] = []; - for (let j = 0; j < n; ++j) { - ans.push(dfs(0, j)); - } - return ans; + return Array.from({ length: n }, (_, j) => dfs(0, j)); } ``` @@ -279,15 +275,45 @@ impl Solution { pub fn find_ball(grid: Vec>) -> Vec { let m = grid.len(); let n = grid[0].len(); - let mut res = vec![0; n]; + let mut ans = vec![0; n]; for i in 0..n { - res[i] = Self::dfs(&grid, 0, i); + ans[i] = Self::dfs(&grid, 0, i); } - res + ans } } ``` +#### JavaScript + +```js +/** + * @param {number[][]} grid + * @return {number[]} + */ +var findBall = function (grid) { + const m = grid.length; + const n = grid[0].length; + const dfs = (i, j) => { + if (i === m) { + return j; + } + if (grid[i][j] === 1) { + if (j === n - 1 || grid[i][j + 1] === -1) { + return -1; + } + return dfs(i + 1, j + 1); + } else { + if (j === 0 || grid[i][j - 1] === 1) { + return -1; + } + return dfs(i + 1, j - 1); + } + }; + return Array.from({ length: n }, (_, j) => dfs(0, j)); +}; +``` + diff --git a/solution/1700-1799/1706.Where Will the Ball Fall/Solution.js b/solution/1700-1799/1706.Where Will the Ball Fall/Solution.js new file mode 100644 index 0000000000000..4c95d25ee3303 --- /dev/null +++ b/solution/1700-1799/1706.Where Will the Ball Fall/Solution.js @@ -0,0 +1,25 @@ +/** + * @param {number[][]} grid + * @return {number[]} + */ +var findBall = function (grid) { + const m = grid.length; + const n = grid[0].length; + const dfs = (i, j) => { + if (i === m) { + return j; + } + if (grid[i][j] === 1) { + if (j === n - 1 || grid[i][j + 1] === -1) { + return -1; + } + return dfs(i + 1, j + 1); + } else { + if (j === 0 || grid[i][j - 1] === 1) { + return -1; + } + return dfs(i + 1, j - 1); + } + }; + return Array.from({ length: n }, (_, j) => dfs(0, j)); +}; diff --git a/solution/1700-1799/1706.Where Will the Ball Fall/Solution.rs b/solution/1700-1799/1706.Where Will the Ball Fall/Solution.rs index f2b7620fd18f0..2522351337a6b 100644 --- a/solution/1700-1799/1706.Where Will the Ball Fall/Solution.rs +++ b/solution/1700-1799/1706.Where Will the Ball Fall/Solution.rs @@ -19,10 +19,10 @@ impl Solution { pub fn find_ball(grid: Vec>) -> Vec { let m = grid.len(); let n = grid[0].len(); - let mut res = vec![0; n]; + let mut ans = vec![0; n]; for i in 0..n { - res[i] = Self::dfs(&grid, 0, i); + ans[i] = Self::dfs(&grid, 0, i); } - res + ans } } diff --git a/solution/1700-1799/1706.Where Will the Ball Fall/Solution.ts b/solution/1700-1799/1706.Where Will the Ball Fall/Solution.ts index d8a275b8dbfbb..c8ad783bb9b91 100644 --- a/solution/1700-1799/1706.Where Will the Ball Fall/Solution.ts +++ b/solution/1700-1799/1706.Where Will the Ball Fall/Solution.ts @@ -17,9 +17,5 @@ function findBall(grid: number[][]): number[] { return dfs(i + 1, j - 1); } }; - const ans: number[] = []; - for (let j = 0; j < n; ++j) { - ans.push(dfs(0, j)); - } - return ans; + return Array.from({ length: n }, (_, j) => dfs(0, j)); }