Skip to the code
cf.
+25 XP

#592 Fewest Hops to the Final Slide

medium

Using the same "skip ahead" feature, from slide i the presenter may jump forward by 1 up to skips[i] slides.

Write fewestHops(skips) that returns the fewest jumps needed to get from the first slide to the last slide, or -1 if the last slide can't be reached. A deck with one slide needs 0 jumps.

Example 1

Input: skips = [1,3,2,1,4]
Output: 2

Jump to slide 1, then to the last slide.

Example 2

Input: skips = [2,1,0,0,3]
Output: -1

Slides 2 and 3 are dead ends that every path runs into.

Constraints

  • 1 <= skips.length <= 100000
  • 0 <= skips[i] <= 1000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).