Problem · Array
Jump Game IV
Learn this problemProblem statement
Given an integer array arr, start at index 0. From index i, one jump may move to i - 1, i + 1, or any index j such that arr[i] == arr[j].
Return the minimum number of jumps needed to reach the last index. Indices outside the array are not valid destinations.
Function
minJumps(arr: int[]) → intExamples
Example 1
arr = [100,-23,-23,404,100,23,23,23,3,404]return = 3One shortest route is index 0 -> 4 -> 3 -> 9.
Example 2
arr = [7]return = 0