English
1345. Jump Game IV
Problem Statement
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]
andi != 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.
Example 1:
Input: arr = [100,-23,-23,404,100,23,23,23,3,404]
Output: 3
Explanation: You need three jumps from index 0 --> 4 --> 3 --> 9. Note that index 9 is the last index of the array.
Example 2:
Input: arr = [7]
Output: 0
Explanation: Start index is the last index. You do not need to jump.
Example 3:
Input: arr = [7,6,9,6,9,6,9,7]
Output: 1
Explanation: You can jump directly from index 0 to index 7 which is last index of the array.
Constraints:
1 <= arr.length <= 5 * 104
-108 <= arr[i] <= 108
Click to open Hints
- Build a graph of n nodes where nodes are the indices of the array and edges for node i are nodes i+1, i-1, j where arr[i] == arr[j].
- Start bfs from node 0 and keep distance. The answer is the distance when you reach node n-1.
Solution:
rs
use std::collections::{HashMap, VecDeque};
impl Solution {
pub fn min_jumps(arr: Vec<i32>) -> i32 {
let mut map = HashMap::new();
for (i, &v) in arr.iter().enumerate() {
map.entry(v).or_insert(vec![]).push(i);
}
let mut q = VecDeque::new();
q.push_back((0, 0_i32));
let mut visited = vec![false; arr.len()];
visited[0] = true;
while let Some((i, mut step)) = q.pop_front() {
if i == arr.len() - 1 {
return step;
}
step += 1;
if let Some(v) = map.remove(&arr[i]) {
for j in v {
if !visited[j] {
visited[j] = true;
q.push_back((j, step));
}
}
}
if i + 1 < arr.len() && !visited[i + 1] {
visited[i + 1] = true;
q.push_back((i + 1, step));
}
if i >= 1 && !visited[i - 1] {
visited[i - 1] = true;
q.push_back((i - 1, step));
}
}
-1
}
}
...