Jump game [2,5,0,0] expecting true


  • 2
    C

    I am failing the test [2,5,0,0] for jump game. How says the expected answer is true. How is this true? When it starts from index 0, it jumps two spaces to index 2, which has a value of 0. It cannot get to the end, so should return false. I am writing in Javascript.

    Here is my code:

    var canJump = function(nums) {
        var currentPos = 0;
        var lastPos = null;
        var repeat = false;
        while (currentPos !== (nums.length - 1)) {
            currentPos = currentPos + nums[currentPos];
            if (currentPos === lastPos) {
                return false;
            } else if (nums[currentPos] === undefined) {
                return true;
            }
            lastPos = currentPos;
        }
        return true;
    };

  • 0

    Read the problem again, you overlooked the word "maximum".


  • 0
    P

    You should read the description on detail. no more 2, not 2.


  • 0
    C

    ok so max spaces to jump, thanks.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.