Menu
CoddyTech

Jump Game

You stand on index 0 of the array nums. From index i you may jump forward by any number of steps from 1 up to nums[i], so nums[i] is your longest jump from there and a 0 means you cannot move. Return true if some sequence of jumps lands on the last index, and false otherwise.

Function

canJump(nums: integer-array) → boolean
numsinteger-array
the longest jump you can make from each index
Returnsboolean
true if you can land on the last index starting from index 0, otherwise false

Constraints

  • 1 ≤ nums.length ≤ 104
  • 0 ≤ nums[i] ≤ 105
  • A jump may be shorter than nums[i], so a long jump never forces you past the last index.

Examples

Input
nums = [2, 0, 3, 1, 0, 2]
Output
true
Explanation
From index 0 you can reach index 1 or 2. Index 1 holds 0 and is a dead end, but index 2 holds 3 and reaches index 5, the last index.

lock icon+18 hidden tests on Submit

challenge icon

Follow-up

Count the different jump sequences that land on the last index, modulo 10^9+7, still in O(n) time.

Reset code
def canJump(nums):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

nums = [2, 0, 3, 1, 0, 2]

Expected

true