-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1345-jump-game-iv.js
69 lines (58 loc) · 1.57 KB
/
1345-jump-game-iv.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* 1345. Jump Game IV
* https://leetcode.com/problems/jump-game-iv/
* Difficulty: Hard
*
* Given an array of integers arr, you are initially positioned at the first index of the array.
*
* In one step you can jump from index i to index:
* - i + 1 where: i + 1 < arr.length.
* - i - 1 where: i - 1 >= 0.
* - j where: arr[i] == arr[j] and i != j.
*
* Return the minimum number of steps to reach the last index of the array.
*
* Notice that you can not jump outside of the array at any time.
*/
/**
* @param {number[]} arr
* @return {number}
*/
var minJumps = function(arr) {
const n = arr.length;
if (n <= 1) return 0;
const valueToIndices = new Map();
for (let i = 0; i < n; i++) {
if (!valueToIndices.has(arr[i])) {
valueToIndices.set(arr[i], []);
}
valueToIndices.get(arr[i]).push(i);
}
const visited = new Set([0]);
let queue = [0];
let result = 0;
while (queue.length) {
const nextQueue = [];
for (const current of queue) {
if (current === n - 1) return result;
if (current + 1 < n && !visited.has(current + 1)) {
visited.add(current + 1);
nextQueue.push(current + 1);
}
if (current - 1 >= 0 && !visited.has(current - 1)) {
visited.add(current - 1);
nextQueue.push(current - 1);
}
for (const next of valueToIndices.get(arr[current]) || []) {
if (!visited.has(next)) {
visited.add(next);
nextQueue.push(next);
}
}
valueToIndices.delete(arr[current]);
}
queue = nextQueue;
result++;
}
return result;
};