Implement the method minJumpsToEnd that returns the fewest jumps needed to reach the last index.
Each array value tells the maximum number of positions you may jump forward from that index.
The task is designed to test careful handling of edge cases, not only the most common input.
- Use only the first
sizeelements. - Return
0whensizeis1. - Return
-1if the end cannot be reached.
Example 1
Input
jumps (int[]) =
[2,3,1,1,4]size (int) =
5Returns (int)
2Example 2
Input
jumps (int[]) =
[2,1,0,3]size (int) =
4Returns (int)
-1Example 3
Input
jumps (int[]) =
[0]size (int) =
1Returns (int)
0Track the current jump range and farthest reachable index. When the current range ends, take one jump.
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run