Jump Game II
Real-world version: Fewest Drone FlightsThe statement here is our own: the same inputs, outputs and technique.
A courier drone sits on the first of a row of rooftop landing pads, indexed from 0 to len(reach) - 1.
You are given an integer array reach. When the drone lifts off from pad i, its remaining battery lets it settle on any pad from i + 1 up to and including i + reach[i], whichever of those pads exist. A single takeoff-and-landing counts as one flight. A pad whose value is 0 has no charging coil, so a drone that lands there can never lift off again.
Return the fewest flights that carry the drone from pad 0 to the last pad. The route planner only ever hands you a row in which the last pad is attainable, and a drone that already starts on the last pad needs no flights at all.
Examples
Example 1
- Input
- reach = [3, 1, 1, 4, 2]
- Output
- 2
One flight of three pads lands on pad 3, then a flight of a single pad finishes the trip. No single flight spans four pads from the start.
Example 2
- Input
- reach = [1, 4, 0, 0, 2, 1]
- Output
- 2
Hop to pad 1, whose charge of 4 covers the two dead pads and lands directly on pad 5.
Example 3
- Input
- reach = [0]
- Output
- 0
The only pad in the row is also the destination.
Constraints
- 1 <= reach.length <= 10^4
- 0 <= reach[i] <= 1000
- The last pad is always attainable from pad 0
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def fewest_flights(reach: list[int]) -> int:public int fewestFlights(int[] reach)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Best Time to Buy and Sell Stock IIReal-world version: Collectible Resale RunMedium
- Jump GameReal-world version: Stepping Stone CrossingMedium
- Non-overlapping IntervalsReal-world version: Drop the Clashing Kiln RunsMedium
- Best Time to Buy and Sell Stock with Transaction FeeReal-world version: Handling Fee ResaleMedium