Skip to content

Commit a104f0c

Browse files
authored
Update 1306-jump-game-iii.js
1 parent 3bb35ff commit a104f0c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

1306-jump-game-iii.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,27 @@ const canReach = function (A, i) {
3131
(!(A[i] = -A[i]) || canReach(A, i + A[i]) || canReach(A, i - A[i]))
3232
)
3333
}
34+
35+
// another
36+
37+
/**
38+
* @param {number[]} arr
39+
* @param {number} start
40+
* @return {boolean}
41+
*/
42+
const canReach = function(arr, start) {
43+
const q = [start]
44+
const s = new Set()
45+
while(q.length) {
46+
const len = q.length
47+
for(let i = 0; i < len; i++) {
48+
const cur = q.shift()
49+
s.add(cur)
50+
if(arr[cur] === 0) return true
51+
if(!s.has(cur + arr[cur])) q.push(cur + arr[cur])
52+
if(!s.has(cur - arr[cur])) q.push(cur - arr[cur])
53+
}
54+
}
55+
return false
56+
};
57+

0 commit comments

Comments
 (0)