Problem 936
Jump Game IV
Hard · 30 pts · Array, Breadth-First Search, Hash Table
Problem
You start at index 0 of an array and want to reach the last index in as few jumps as possible. From index i you may jump to i + 1, to i - 1 (staying inside the array), or to any index j with ARR[j] = ARR[i]. Run a breadth-first search over indices. Group the indices by value; after using a value's group once, clear it, so that each group is scanned only once and the search stays linear.
Input
The first line contains N. The second line contains the N values.
Output
Print the minimum number of jumps to reach the last index.
Constraints
1 <= N <= 50000; -10^8 <= ARR[i] <= 10^8
Example input
10 100 -23 -23 404 100 23 23 23 3 404
Example output
3
Explanation
In 100 -23 -23 404 100 23 23 23 3 404, jump from index 0 to index 4 (same value 100), then to index 3 (one step left), then to index 9 (same value 404). That is 3 jumps.