All problems
0088MediumArrayDynamic ProgrammingGreedy

Fewest Drone Flights

Tracked in this browser only
Write code

Trains the technique from

LeetCode 45Jump Game II

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def fewest_flights(reach: list[int]) -> int:
Java
public int fewestFlights(int[] reach)
September 7
Apply