Skip to content

Commit 110f7ad

Browse files
authored
Create 1706-where-will-the-ball-fall.js
1 parent be167e9 commit 110f7ad

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

1706-where-will-the-ball-fall.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number[]}
4+
*/
5+
const findBall = function (grid) {
6+
const res = new Array(grid[0].length).fill(0)
7+
for (let i = 0; i < res.length; i++) {
8+
let start = i
9+
let state = 1
10+
for (let j = 0; j < grid.length; j++) {
11+
if (grid[j][start] === 1) {
12+
if (start >= grid[0].length - 1 || grid[j][start + 1] === -1) {
13+
state = -1
14+
break
15+
}
16+
start++
17+
} else {
18+
if (start <= 0 || grid[j][start - 1] == 1) {
19+
state = -1
20+
break
21+
}
22+
start--
23+
}
24+
}
25+
res[i] = state === -1 ? state : start
26+
}
27+
return res
28+
}

0 commit comments

Comments
 (0)