#1345
Jump Game IV
candidate master · 1525 · lc hard +32 · verified · 46.2% accepted · 3,865 likes · top 30%
Description
You start at index 0 of the integer array arr. In one step from index i, you may move to i + 1, i - 1, or any index j where arr[j] == arr[i] and j != i. Return the minimum number of steps to reach the last index.
You may never jump outside the array.
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.
Code
1
2
3