Source: https://leetcode.com/problems/jump-game/

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position. Return true if you can reach the last index, or false otherwise.

/**
* @param {number[]} nums
* @return {boolean}
*/
// Recursive Approach
// Starting from the first index, can do DFS based on the value of the element
// Assume nums.length >= 1; and nums[i] can be 0 i.e. [0,1,2,3] -> false
// Edge Case: [0] -> true, [2] -> true
// Base Case: if startIndex === lastIndex, we made it and jumped to the end -> return true
// Loop from i = 1 to nums[i]
// Recursively try jumping i steps with new start index and if it's possible through that path, return true
// return false as we tried all possible jumps and failed
// O(n^n) time since at each index can potentially jump n different ways; O(n) function stack space
var canJump = function(nums) {
const lastIndex = nums.length - 1;
const canJumpRec = (startIndex) => {
if (startIndex === lastIndex) {
return true;
}
for (let i = 1; i <= nums[startIndex]; i++) {
if (canJumpRec(startIndex + i)) {
return true;
}
}
return false;
};
return canJumpRec(0);
};