Jump Game IV

Hard

Topics
ArrayBFSHash Table

Given an integer array nums, you start at index 0. From index i you can move to i+1, i-1, or any index j with nums[j] == nums[i] (all within bounds). Return the minimum number of steps to reach the last index.

Example 1

Input:  nums = [100,-23,-23,404,100,23,23,23,3,404]
Output: 3

Example 2

Input:  nums = [7]
Output: 0

Example 3

Input:  nums = [7,6,9,6,9,6,9,7]
Output: 1

Constraints

  • 1 <= nums.length <= 5*10^4
  • -10^8 <= nums[i] <= 10^8
Run ⌘' · Submit ⌘⏎