Jump Game

Medium#55

You stand on the first cell of an array. Each number is the longest jump you may make from its cell; any shorter jump is fine too. Can you get to the last cell? Our example: [1, 4, 2, 0, 0, 3, 0, 1] — yes: 0 → 1 → 5 → 7. The zeros at indices 3 and 4 do not matter, as you can jump over them.

input
nums: an array of integers. You stand on index 0; from index i you may jump forward by any number of places from 1 to nums[i].
returns
true if some sequence of jumps reaches the last index; otherwise false.
constraints
  • 1 ≤ nums.length ≤ 5000
  • 0 ≤ nums[i] ≤ 10⁵
our 14 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[]} nums
 * @return {boolean}
 */
function canJump(nums) {

}
Ctrl ↵
format
reset

saved in this browser

14 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more greedy problems

Pattern “Greedy” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.