There are multiple ways through the array.
The one you described is:
2 1 1 2 2 1 6 jumps
┌───┬─┬─┬───┬───┬─┐
2 3 1 1 2 4 2 0 1 1
But you can also do:
1 3 2 2 1 5 jumps
┌─┬─────┬───┬───┬─┐
2 3 1 1 2 4 2 0 1 1
↑
jump 1 forward, not 2
You can find an optimal solution as:
1 3 1 4 4 jumps
┌─┬─────┬─┬───────┐
2 3 1 1 2 4 2 0 1 1
For any given array, there may be more than one optimal solution.
The trick is to explore them all
Every time you have a decision to make, make them all.
For example, given our first jump of 2
, we could also choose to jump 1
. Whenever you have a choice like this you will need to build yourself a recursive solution.
Conveniently, you only need to return the minimal number of jumps. You do not actually have to return the jump path. So your function can be written to return exactly that information:
int minJumps( int array[], int size )
The thing that will change is your starting index. You can add that as an additional argument:
int minJumps( int array[], int size, int index = 0 )
Or you can just adjust the array
value when you recurse:
... = minJumps( array + n, size - n );
Either way you do the same thing: index a smaller and smaller array.
For example, you can easily answer for this array:
1 zero jumps. (You are at the end of the array)
And this array:
0 zero jumps. (You are at the end of the array)
How about this array:
1 1
Or this array:
5 1
The answer to both is 1 jump, because if we choose a jump-by-1 we get the result of 0 == end of array.
For this array, though:
0 1
The answer is no jumps.
So can you answer for this array?
1 1 1
Or this array?
1 5 1
How about this array?
1 0 1
Aaaannnd... how about these arrays?
2 1 1
2 0 1
The decision factor here is where recursion is useful. Each time you recurse you are working on a smaller array. For the penultimate example, I can get to the end of the array with two possible paths:
┌───┐ 1 jump
2 1 1 2 straight to end works. return 1 jump
┌─┬─┐ 2 jumps
2 1 1 recurse to get 1 jump for the end of the array. return that + 1.
↑
decision point
But there is only one way with:
┌───┐ 1 jump
2 0 1 2 straight to end works. return 1 jump
┌─┬─┐ no jumps
2 0 1 recurse to get -1 for the end of the array.
↑
decision point
With both of these examples we see that the minimum number of jumps is if we choose the jump-by-2.
So, each time you have a decision, take them all. Ignore the bad ones (stuff that returns -1
) and choose the smallest number of jumps every time.
Oh, don’t forget to not jump past the end of the array. So
5 1
↑
decision point
Since the array is size 2
, you cannot jump more than 1 forward.
Putting it together
Here are the conditions given you:
- You have one element in the array. Return
0
.
- The current element is zero. Return
-1
. (Cannot advance.)
- The current element is n. Recurse for each 1,2,...,n, making sure that you do not exceed the bounds of the array. Return the smallest value you got from your recursive calls.
That’s it! It takes some time to work your mind around recursion, but the main idea is to solve for the simplest case (a single element array), then solve for that with one more element. Or, if I can do a thing to the first element of an array in relation to the rest of the array, then I can do it to the whole array.
Edit: I have written this answer assuming that you have not been given instruction about Graph Theory. If you have, then do as bitmask instructs and look at the algorithms you have been studying. At least one of them will apply to traversing this graph.