1
var findBall = function (grid) {
2
let m = grid.length,
3
n = grid[0].length,
4
ans = [];
5
for (let start = 0; start < n; start++) {
6
// Iterate through the different starting conditions
7
let j = start;
8
for (let i = 0; i < m; i++) {
9
// Then iterate downward from grid[i][j]
10
let dir = grid[i][j]; // Compare the direction of the current cell to the direction
11
if (dir === grid[i][j + dir])
12
j += dir; // of the cell on the slant side and move that way if matched
13
else (i = m), (j = -1); // Otherwise jump to the loop's end and set j to the fail value
14
}
15
ans[start] = j; // Update the answer
16
}
17
return ans; // Return the completed answer
18
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0