forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJump Game III.js
41 lines (32 loc) · 946 Bytes
/
Jump Game III.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Runtime: 84 ms (Top 85.29%) | Memory: 51.6 MB (Top 46.69%)
var canReach = function(arr, start) {
const N = arr.length;
const visited = new Set();
const queue = [[start]];
const diff = [1, -1];
while (queue.length > 0) {
const nodes = queue.pop();
const newNodes = [];
for (const node of nodes) {
const jump = arr[node];
if (jump === 0) {
return true;
}
for (const d of diff) {
const newNode = node + (jump * d);
if (newNode < 0 || newNode >= N) {
continue;
}
if (visited.has(newNode)) {
continue;
}
visited.add(newNode);
newNodes.push(newNode);
}
}
if (newNodes.length > 0) {
queue.push(newNodes);
}
}
return false;
};