Jump Game II

Medium#45

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. The last cell can surely be reached. In how few jumps? Our example: [2, 4, 1, 2, 3, 1, 1, 0] — 3 jumps: 0 → 1 → 4 → 7. Two are not enough: after two jumps you get no farther than index 5.

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]. The last index can be reached.
returns
The fewest jumps that reach the last index.
constraints
  • 1 ≤ nums.length ≤ 5000
  • 0 ≤ nums[i] ≤ 1000
  • the last index can be reached
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 {number}
 */
function jump(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.