Jump Game II
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 indexiyou may jump forward by any number of places from 1 tonums[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
- 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
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more greedy problems
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.