Problem 531854 · medium · Phase 05 Advanced Algorithms & Graphs

Fewest Hops Across the Stones

greedy · BFS levels · arrays

Stones lie in a row, numbered 0 .. n-1. From stone i a frog can hop forward to any stone i + 1 .. i + power[i]. Starting on stone 0, return the minimum number of hops needed to land on stone n - 1, or -1 if it is impossible.

Examples

Input:  power = [2, 1, 3, 1, 1, 2, 1]
Output: 3
Explanation: 0 -> 2 -> 5 -> 6.
Input:  power = [2, 1, 3, 1, 1, 0, 1]
Output: -1
Explanation: Nothing gets past stone 5.

Constraints

  • 1 <= len(power) <= 10**5, 0 <= power[i] <= 10**5.
  • A single stone needs 0 hops.
  • Target complexity: O(n) time, O(1) extra space.

Goals

  • View each hop count as a layer of reachable stones
  • Track the furthest stone reachable with one more hop
  • Return -1 when the far bank cannot be reached
Starting Python…