I have this code for minimum jumps to reach at end
const countMinJumps = function(jumps) {
function countMinJumpsRecursive(jumps, currentIndex) {
if (currentIndex === jumps.length - 1) return 0;
if (jumps[currentIndex] === 0) return Number.MAX_VALUE;
let totalJumps = Number.MAX_VALUE;
let start = currentIndex + 1;
const end = currentIndex + jumps[currentIndex];
while (start < jumps.length && start <= end) {
// jump one step and recurse for the remaining array
const minJumps = countMinJumpsRecursive(jumps, start);
if (minJumps !== Number.MAX_VALUE) {
totalJumps = Math.min(totalJumps, minJumps + 1);
}
start = start + 1
}
return totalJumps; } return countMinJumpsRecursive(jumps, 0); };
console.log(Minimum jumps needed: ---> ${countMinJumps([2, 1, 3])}); console.log(Minimum jumps needed: ---> ${countMinJumps([1, 1, 3, 6, 9, 3, 0, 1, 3])});
I want to know the logic behind adding the currentIndex into the value at currentIndex that is:
const end = currentIndex + jumps[currentIndex];
why are we adding the currentIndex here?