Array Jumps
ARRAY
DYNAMIC PROGRAMMING
Problem
Given an array of non-negative integers nums, where nums[i] denotes the maximum number of steps you can jump forward from index i, determine the minimum number of jumps you need to reach the last index. If it's not possible to reach the last index, return -1.
Examples
minJumps([2,3,1,1,4]) // returns 2 // The minimum number of jumps to reach the end is 2. // Jump from index 0 to 1 (as jump length at index 1 is 3), // then jump from index 1 to 4 (as jump length at index 4 is 4). minJumps([2,1,0,3]) // returns -1 // There is no way to reach the end of the desert because // at index 2 the jump length is 0, making further progress impossible.
Loading...