Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Jump Game

Greedymedium

Given `nums` where each value is the maximum jump length from that index, implement `canJump(nums)` returning whether you can reach the last index starting from index 0.

Sample tests

Input: canJump([2,3,1,1,4])
Output: true
Input: canJump([3,2,1,0,4])
Output: false

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Trying every jump path is exponential; the greedy reach is O(n).

Learning resources

  • Wikipedia: Greedy algorithm
Approach & explanation (try first)

Carrying the farthest reach in one pass decides reachability in O(n) time.

Loading...
⌘/Ctrl + Enter

Run your code to see results.