-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
39 lines (38 loc) · 1.17 KB
/
solution.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
/**
* @param {number[]} arr
* @return {number}
*/
var minJumps = function (arr) {
const groupByNode = arr.reduce((obj, item, index) => {
if (!obj[item]) {
obj[item] = [];
}
obj[item].push(index);
return obj;
}, {});
const minSteps = new Array(arr.length).fill(Infinity);
minSteps[0] = 0;
const queue = [
0, ];
while (queue.length) {
const index = queue.shift();
// 探索临边
if (index > 0 && minSteps[index - 1] > minSteps[index] + 1) {
minSteps[index - 1] = minSteps[index] + 1;
queue.push(index - 1);
}
if (index < arr.length - 1 && minSteps[index + 1] > minSteps[index] + 1) {
minSteps[index + 1] = minSteps[index] + 1;
queue.push(index + 1);
}
const sameIndexs = groupByNode[arr[index]];
for (let i = 0; i < sameIndexs.length; i++) {
const rIndex = sameIndexs[i];
if (minSteps[rIndex] > minSteps[index] + 1) {
minSteps[rIndex] = minSteps[index] + 1;
queue.push(rIndex);
}
}
}
return minSteps[arr.length - 1];
};