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/1700-1799/1706.Where Will the Ball Fall/README_EN.md
+42-16
Original file line number
Diff line number
Diff line change
@@ -80,18 +80,18 @@ Ball b4 is dropped at column 4 and will get stuck on the box between column 2 an
80
80
81
81
<!-- solution:start -->
82
82
83
-
### Solution 1: Case Discussion + DFS
83
+
### Solution 1: Case Analysis + DFS
84
84
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:
86
86
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.
91
91
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.
93
93
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.
95
95
96
96
<!-- tabs:start -->
97
97
@@ -247,11 +247,7 @@ function findBall(grid: number[][]): number[] {
247
247
returndfs(i+1, j-1);
248
248
}
249
249
};
250
-
const ans:number[] = [];
251
-
for (let j =0; j<n; ++j) {
252
-
ans.push(dfs(0, j));
253
-
}
254
-
returnans;
250
+
returnArray.from({ length: n }, (_, j) =>dfs(0, j));
255
251
}
256
252
```
257
253
@@ -279,15 +275,45 @@ impl Solution {
279
275
pubfnfind_ball(grid:Vec<Vec<i32>>) ->Vec<i32> {
280
276
letm=grid.len();
281
277
letn=grid[0].len();
282
-
letmutres=vec![0; n];
278
+
letmutans=vec![0; n];
283
279
foriin0..n {
284
-
res[i] =Self::dfs(&grid, 0, i);
280
+
ans[i] =Self::dfs(&grid, 0, i);
285
281
}
286
-
res
282
+
ans
287
283
}
288
284
}
289
285
```
290
286
287
+
#### JavaScript
288
+
289
+
```js
290
+
/**
291
+
* @param{number[][]}grid
292
+
* @return{number[]}
293
+
*/
294
+
varfindBall=function (grid) {
295
+
constm=grid.length;
296
+
constn= grid[0].length;
297
+
constdfs= (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
+
returndfs(i +1, j +1);
306
+
} else {
307
+
if (j ===0|| grid[i][j -1] ===1) {
308
+
return-1;
309
+
}
310
+
returndfs(i +1, j -1);
311
+
}
312
+
};
313
+
returnArray.from({ length: n }, (_, j) =>dfs(0, j));
0 commit comments