Jump Game

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write canJump(nums): nums holds 1 to 100,000 whole numbers, none negative. You start at index 0, and from index i you may jump forward by any number of places from 1 up to nums[i]. Return true if you can reach the last index, or false if not; with a single entry you're already there. Don't change nums. For example, canJump([2, 3, 1, 1, 4]) is true, but canJump([3, 2, 1, 0, 4]) is false, as every route lands on the 0. The checks include 100,000 entries with long jumps, so marking every place each jump can reach is too slow: track the farthest index you can reach so far.

Your code must pass

  • Returns true when the end can be reached
  • Returns false when every route gets stuck
  • Handles a single entry
  • Is fast enough for 100,000 entries