What about 0 values?

  • 1

    My main question about this problem is: What about array elements with value 0?
    I can think of at least one situation where you would never be able to reach the end if you ran into a zero, namely input [2,1,0,5,2], and I am sure others can be found.
    As there are solutions that have passed this test, I can only assume that:

    • either trial-and-error have lead to the correct way of handling the situation;
    • or none of the problem inputs actually have such a solution.

    So, which one is it? And, to make it clear, how would one be expected to handle 0? Can we just assume that we would never hit a situation where 0 stops us from jumping on?

    Many Thanks,

  • 3

    0 simply means there is no steps for you to jump from that index, therefore if you land on that index, you can't move. Your example [2,1,0,5,2] means you can never reach the goal.

    For Jump Game Part II, you can just assume that you are always able to reach the goal, since we are only interested in the minimum number of jumps to reach the goal. On the other hand, in Part I you will write a function canJump to determine if you can reach the goal.

  • 0

    Thank you, I was not aware of that.
    I have started doing the problems in a random order and have not run into Jump Game I yet.

Log in to reply

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