Skip to content
BytePatterns

Fewest Hops to the End

MediumGreedy#greedy#reach-frontier~20m

Problem

Each position in a list holds the maximum number of steps you may hop forward from it. You start at position 0 and the last position is always reachable. Return the smallest number of hops that gets you there.

Examples

Input:  nums = [2, 3, 1, 1, 4]
Output: 2
Why:    hop to index 1, then straight to the end
Input:  nums = [2, 1, 1, 1, 1]
Output: 3
Why:    no single hop covers more than two positions here
Input:  nums = [0]
Output: 0
Why:    edge case, you already stand on the last position

Hints

0 / 3

Stuck on the idea rather than the code? Jump Game covers it.