Watch 10 video solutions for Jump Game IV, a hard level problem involving Array, Hash Table, Breadth-First Search. This walkthrough by codestorywithMIK has 7,703 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
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] <= 108Problem Overview: You start at index 0 of an array and want to reach the last index using the minimum number of jumps. From index i, you can move to i + 1, i - 1, or any other index j where arr[i] == arr[j]. The challenge is finding the shortest path while avoiding repeated work when many elements share the same value.
Approach 1: Breadth-First Search (BFS) (Time: O(n), Space: O(n))
Treat the array as an implicit graph where each index is a node. From a node i, edges exist to i - 1, i + 1, and all indices with the same value. Build a hash map from value to list of indices, then run BFS starting from index 0. BFS guarantees the first time you reach the last index is the minimum number of jumps. Use a visited set to avoid revisiting indices. The expensive part is repeatedly scanning lists of equal values, which can cause redundant work if many elements share the same value. This approach demonstrates the graph modeling technique commonly used with Breadth-First Search problems.
Approach 2: Optimized BFS with Lazy Clearing (Time: O(n), Space: O(n))
The key optimization is preventing repeated traversal of equal-value groups. Use a hash table that maps each value to its indices. During BFS, when you process all jumps for a value (for example, every index where arr[i] == x), immediately clear that list from the map. This "lazy clearing" ensures the group is processed only once. Each index is pushed into the queue at most once, and each value group is scanned once, keeping the total complexity linear. This approach combines array traversal, fast hash table lookups, and BFS layer expansion to efficiently compute the shortest path.
Recommended for interviews: Interviewers expect the optimized BFS solution. Modeling the problem as a graph and using BFS shows you recognize the shortest-path pattern. Mentioning the naive BFS first demonstrates understanding of the graph structure, but the optimized version with lazy clearing proves you can eliminate redundant scans and achieve true O(n) performance.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Breadth-First Search (Basic) | O(n) | O(n) | Understanding the graph model of the problem and building the initial BFS solution. |
| Optimized BFS with Lazy Clearing | O(n) | O(n) | Preferred interview solution. Eliminates repeated scans of equal-value indices for true linear performance. |