55. Jump Game - Medium

 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.

Example:
Input: nums = [2,3,1,1,4]

Output: true

Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: nums = [3,2,1,0,4]

Output: false

Explanation: You will always arrive at index 3 no matter wht. Its maximum jump length is 0, which makes it impossible to reach the last index.

Constraints:

1 <= nums.length <= 10**4

0 <= nums[i] <= 10**5

Solution:

var canJump = function(nums) {

    let goal = nums.length - 1;

    for (let i = nums.length - 2; i >= 0; i--) {

        if (i + nums[i] >= goal) {

            goal = i;

        }

    return goal === 0;

}


Thoughts:
Too difficult this problem. I would have never thought to start at the end and move the goal and see if it could reach the first index. If it can then return true else false. It's one of those difficult problems where you need to know the technique in order to solve it. I tried to think of a solution and didn't have a clue within 15 minutes, I looked for the solution. I kind of understand what's going on but would never have come up with it on my own. Wish the problem had hints. I'd give my confidence rating a 2/10. I'm not sure if I should skip these mediums and go back to easy ones.

Comments

Popular posts from this blog

28. Find the Index of the First Occurence in a String - Easy

121. Best Time to Buy and Sell Stock - Easy

58. Length of Last Word - Easy