Jump Game II

MediumGreedyGraph BFS and DFS

You are given an array of non-negative integers nums and start at index 0. From index i you may jump forward to any index from i + 1 up to i + nums[i].

Return the minimum number of jumps needed to reach the last index. The tests guarantee that the last index can be reached.

Constraints

  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= 1000
  • The last index is always reachable.

Examples

Example 1

Input: nums = [2,4,1,1,1,1]
Output: 2

Jump 1 step to index 1, then 4 steps from index 1 to the last index. One jump can't do it: from index 0 you reach index 2 at most.

Example 2

Input: nums = [1,1,1,1]
Output: 3

Every value is 1, so you must step on every index: 3 jumps.

Example 3

Input: nums = [0]
Output: 0

You already stand on the last index.

JavaScript you'll use: Numbers, Math and integer tricks Arrays: create, read, update

/**
 * @param {number[]} nums
 * @return {number}
 */
function jump(nums) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.