Skip to content

feat: add solutions to lc problem: No.1706 #4064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions solution/1700-1799/1706.Where Will the Ball Fall/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. 球位于最右一列,并且此单元格挡板将球导向右侧
Expand All @@ -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}$ 的行数和列数。

<!-- tabs:start -->

Expand Down Expand Up @@ -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));
}
```

Expand Down Expand Up @@ -281,15 +277,45 @@ impl Solution {
pub fn find_ball(grid: Vec<Vec<i32>>) -> Vec<i32> {
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));
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
58 changes: 42 additions & 16 deletions solution/1700-1799/1706.Where Will the Ball Fall/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@ Ball b4 is dropped at column 4 and will get stuck on the box between column 2 an

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand Down Expand Up @@ -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));
}
```

Expand Down Expand Up @@ -279,15 +275,45 @@ impl Solution {
pub fn find_ball(grid: Vec<Vec<i32>>) -> Vec<i32> {
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));
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
25 changes: 25 additions & 0 deletions solution/1700-1799/1706.Where Will the Ball Fall/Solution.js
Original file line number Diff line number Diff line change
@@ -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));
};
6 changes: 3 additions & 3 deletions solution/1700-1799/1706.Where Will the Ball Fall/Solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ impl Solution {
pub fn find_ball(grid: Vec<Vec<i32>>) -> Vec<i32> {
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
}
}
6 changes: 1 addition & 5 deletions solution/1700-1799/1706.Where Will the Ball Fall/Solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Loading