InferenceAI

Enter at least two characters to search.

Start free

Fewest Jumps to the End

mediumGreedy & Intervals25 min25 XP

From index i you can jump up to nums[i] steps forward. Return the minimum number of jumps to reach the last index, or -1 if it is unreachable.

Requirements

  • O(n)O(n) greedy, treating jumps like BFS levels.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[2,3,1,1,4]]
TASKfewest_jumps()
EXPECTED OUTPUT2

Start with the input values shown in this example.

Example 1 of 3

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

Output: 2

Explanation: classic

Input: nums = [2,3,0,1,4]

Output: 2

Explanation: zero in the way

Input: nums = [0]

Output: 0

Explanation: already there

Hint 1

Track the furthest index reachable with the current number of jumps; when you pass the end of the current level, take another jump.

Topics

DSAGreedy & IntervalsPython