Array Hopper II
Medium
Given an array A of non-negative integers, you are initially positioned at index 0 of the array. A[i] means the maximum jump distance from index i (you can only jump towards the end of the array). Determine the minimum number of jumps you need to reach the end of array. If you can not reach the end of the array, return -1.
Assumptions
The given array is not null and has length of at least 1.
Examples
{3, 3, 1, 0, 4}, the minimum jumps needed is 2 (jump to index 1 then to the end of array)
{2, 1, 1, 0, 2}, you are not able to reach the end of array, return -1 in this case.
Solution: Back to front scan. Memoized
At each index, check forward array[index] steps
set M[index] to lowest value + 1
TC: O(N^2) double for loop
SC: O(N) Memoized
Last updated
Was this helpful?