← Back to Visualizer Gallery
Traversal · State-Space BFS LeetCode 1345

Jump Game IV / teleportation & bucket eviction

Jumping forward, backward, or teleporting to identical values: the crucial optimization is clearing the value-bucket immediately after its first expansion to prevent $O(n^2)$ duplicate scans.

Execution Stage

idle
INVARIANT Bucket Eviction Invariant: Once an identical-value jump list map.get(arr[u]) is traversed, it MUST be cleared (.clear()). All matching indices have entered the queue at distance steps + 1; re-scanning them wastes $O(n)$ time.
Steps 0
Active Index 0
Queue Size 0
Press Run to trace Jump Game IV BFS.
0 / 0
Speed

Java · Reference Algorithm

INTERVIEW SCRIPT

How to Present Jump Game IV to an Interviewer

1. Graph Modeling

"The array indices $0 \dots n-1$ are vertices. From index $i$, directed unit-weight edges point to $i - 1$, $i + 1$, and all $j$ sharing the same value $arr[j] == arr[i]$."

2. The Bucket Eviction Trick

"The trap is $O(n^2)$ TLE when many elements share identical values. After expanding map.get(arr[i]), we must invoke .clear()! Re-scanning this bucket later yields no shorter distance."

3. BFS Level Distance

"Because every jump type has unit cost 1, standard level-order BFS finds the minimal jump count to index $n-1$ on first arrival."

4. Complexity Guarantee

"With bucket eviction, each index is added to the queue at most once, and each value bucket is scanned exactly once. Time is strictly $O(n)$, space is $O(n)$."

Jump Game IV (LC 1345) — BFS with bucket eviction · Time $O(n)$, Space $O(n)$.