-
Notifications
You must be signed in to change notification settings - Fork 481
/
1345.js
40 lines (35 loc) · 1.01 KB
/
1345.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
var minJumps = function(arr) {
let g = new Map(), values = new Map();
let n = arr.length, step = 0;
for (let i = 0; i < n; i++) {
if (!g.has(arr[i])) g.set(arr[i], []);
g.get(arr[i]).push(i);
}
let vis = new Array(n); vis.fill(0);
let q = [0];
while (q.length) {
let k = q.length;
while (k--) {
let pre = q.shift();
if (pre == n - 1) return step;
if (pre - 1 >= 0 && vis[pre - 1] == 0) {
vis[pre - 1] = 1;
q.push(pre - 1);
}
if (pre + 1 < n && vis[pre + 1] == 0) {
vis[pre + 1] = 1;
q.push(pre + 1);
}
if (values.has(arr[pre])) continue;
values.set(arr[pre], 1);
for (let i of g.get(arr[pre])) {
if (vis[i] == 0) {
vis[i] = 1;
q.push(i);
}
}
}
step++;
}
return step;
};