Skip to content

Commit 3cdcd89

Browse files
authored
feat: add solutions to lc problem: No.1706 (#4064)
No.1706.Where Will the Ball Fall
1 parent b247927 commit 3cdcd89

File tree

5 files changed

+107
-34
lines changed

5 files changed

+107
-34
lines changed

solution/1700-1799/1706.Where Will the Ball Fall/README.md

+36-10
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ b4 球开始放在第 4 列上,会卡在第 2、3 列和第 1 行之间的 "V"
8484

8585
### 方法一:分情况讨论 + DFS
8686

87-
我们可以使用 DFS 来模拟球的运动过程,设计一个函数 $dfs(i, j)$,表示球从第 $i$ 行第 $j$ 列出发,最终会落在第几列。对于以下情况,球会卡住:
87+
我们可以使用 DFS 来模拟球的运动过程,设计一个函数 $\textit{dfs}(i, j)$,表示球从第 $i$ 行第 $j$ 列出发,最终会落在第几列。对于以下情况,球会卡住:
8888

8989
1. 球位于最左一列,并且球所在的单元格单元格挡板将球导向左侧
9090
1. 球位于最右一列,并且此单元格挡板将球导向右侧
@@ -93,7 +93,7 @@ b4 球开始放在第 4 列上,会卡在第 2、3 列和第 1 行之间的 "V"
9393

9494
如果满足以上任意一种情况,我们就可以判断球会卡住,返回 $-1$。否则,我们就可以继续递归地寻找球的下一个位置。最后,如果球到了最后一行,我们就可以返回当前列的编号。
9595

96-
时间复杂度 $O(m \times n)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别是数组 $grid$ 的行数和列数。
96+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别是数组 $\textit{grid}$ 的行数和列数。
9797

9898
<!-- tabs:start -->
9999

@@ -249,11 +249,7 @@ function findBall(grid: number[][]): number[] {
249249
return dfs(i + 1, j - 1);
250250
}
251251
};
252-
const ans: number[] = [];
253-
for (let j = 0; j < n; ++j) {
254-
ans.push(dfs(0, j));
255-
}
256-
return ans;
252+
return Array.from({ length: n }, (_, j) => dfs(0, j));
257253
}
258254
```
259255

@@ -281,15 +277,45 @@ impl Solution {
281277
pub fn find_ball(grid: Vec<Vec<i32>>) -> Vec<i32> {
282278
let m = grid.len();
283279
let n = grid[0].len();
284-
let mut res = vec![0; n];
280+
let mut ans = vec![0; n];
285281
for i in 0..n {
286-
res[i] = Self::dfs(&grid, 0, i);
282+
ans[i] = Self::dfs(&grid, 0, i);
287283
}
288-
res
284+
ans
289285
}
290286
}
291287
```
292288

289+
#### JavaScript
290+
291+
```js
292+
/**
293+
* @param {number[][]} grid
294+
* @return {number[]}
295+
*/
296+
var findBall = function (grid) {
297+
const m = grid.length;
298+
const n = grid[0].length;
299+
const dfs = (i, j) => {
300+
if (i === m) {
301+
return j;
302+
}
303+
if (grid[i][j] === 1) {
304+
if (j === n - 1 || grid[i][j + 1] === -1) {
305+
return -1;
306+
}
307+
return dfs(i + 1, j + 1);
308+
} else {
309+
if (j === 0 || grid[i][j - 1] === 1) {
310+
return -1;
311+
}
312+
return dfs(i + 1, j - 1);
313+
}
314+
};
315+
return Array.from({ length: n }, (_, j) => dfs(0, j));
316+
};
317+
```
318+
293319
<!-- tabs:end -->
294320

295321
<!-- solution:end -->

solution/1700-1799/1706.Where Will the Ball Fall/README_EN.md

+42-16
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,18 @@ Ball b4 is dropped at column 4 and will get stuck on the box between column 2 an
8080

8181
<!-- solution:start -->
8282

83-
### Solution 1: Case Discussion + DFS
83+
### Solution 1: Case Analysis + DFS
8484

85-
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:
85+
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:
8686

87-
1. The ball is in the leftmost column, and the cell's vane directs the ball to the left.
88-
2. The ball is in the rightmost column, and the cell's vane directs the ball to the right.
89-
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.
90-
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.
87+
1. The ball is in the leftmost column, and the cell's diagonal directs the ball to the left.
88+
2. The ball is in the rightmost column, and the cell's diagonal directs the ball to the right.
89+
3. The cell's diagonal directs the ball to the right, and the adjacent cell to the right directs the ball to the left.
90+
4. The cell's diagonal directs the ball to the left, and the adjacent cell to the left directs the ball to the right.
9191

92-
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.
92+
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.
9393

94-
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.
94+
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.
9595

9696
<!-- tabs:start -->
9797

@@ -247,11 +247,7 @@ function findBall(grid: number[][]): number[] {
247247
return dfs(i + 1, j - 1);
248248
}
249249
};
250-
const ans: number[] = [];
251-
for (let j = 0; j < n; ++j) {
252-
ans.push(dfs(0, j));
253-
}
254-
return ans;
250+
return Array.from({ length: n }, (_, j) => dfs(0, j));
255251
}
256252
```
257253

@@ -279,15 +275,45 @@ impl Solution {
279275
pub fn find_ball(grid: Vec<Vec<i32>>) -> Vec<i32> {
280276
let m = grid.len();
281277
let n = grid[0].len();
282-
let mut res = vec![0; n];
278+
let mut ans = vec![0; n];
283279
for i in 0..n {
284-
res[i] = Self::dfs(&grid, 0, i);
280+
ans[i] = Self::dfs(&grid, 0, i);
285281
}
286-
res
282+
ans
287283
}
288284
}
289285
```
290286

287+
#### JavaScript
288+
289+
```js
290+
/**
291+
* @param {number[][]} grid
292+
* @return {number[]}
293+
*/
294+
var findBall = function (grid) {
295+
const m = grid.length;
296+
const n = grid[0].length;
297+
const dfs = (i, j) => {
298+
if (i === m) {
299+
return j;
300+
}
301+
if (grid[i][j] === 1) {
302+
if (j === n - 1 || grid[i][j + 1] === -1) {
303+
return -1;
304+
}
305+
return dfs(i + 1, j + 1);
306+
} else {
307+
if (j === 0 || grid[i][j - 1] === 1) {
308+
return -1;
309+
}
310+
return dfs(i + 1, j - 1);
311+
}
312+
};
313+
return Array.from({ length: n }, (_, j) => dfs(0, j));
314+
};
315+
```
316+
291317
<!-- tabs:end -->
292318

293319
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number[]}
4+
*/
5+
var findBall = function (grid) {
6+
const m = grid.length;
7+
const n = grid[0].length;
8+
const dfs = (i, j) => {
9+
if (i === m) {
10+
return j;
11+
}
12+
if (grid[i][j] === 1) {
13+
if (j === n - 1 || grid[i][j + 1] === -1) {
14+
return -1;
15+
}
16+
return dfs(i + 1, j + 1);
17+
} else {
18+
if (j === 0 || grid[i][j - 1] === 1) {
19+
return -1;
20+
}
21+
return dfs(i + 1, j - 1);
22+
}
23+
};
24+
return Array.from({ length: n }, (_, j) => dfs(0, j));
25+
};

solution/1700-1799/1706.Where Will the Ball Fall/Solution.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ impl Solution {
1919
pub fn find_ball(grid: Vec<Vec<i32>>) -> Vec<i32> {
2020
let m = grid.len();
2121
let n = grid[0].len();
22-
let mut res = vec![0; n];
22+
let mut ans = vec![0; n];
2323
for i in 0..n {
24-
res[i] = Self::dfs(&grid, 0, i);
24+
ans[i] = Self::dfs(&grid, 0, i);
2525
}
26-
res
26+
ans
2727
}
2828
}

solution/1700-1799/1706.Where Will the Ball Fall/Solution.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,5 @@ function findBall(grid: number[][]): number[] {
1717
return dfs(i + 1, j - 1);
1818
}
1919
};
20-
const ans: number[] = [];
21-
for (let j = 0; j < n; ++j) {
22-
ans.push(dfs(0, j));
23-
}
24-
return ans;
20+
return Array.from({ length: n }, (_, j) => dfs(0, j));
2521
}

0 commit comments

Comments
 (0)