InferenceAI

Enter at least two characters to search.

Start free

Fewest Fuel Stops

hardHeaps40 min50 XP

A car starts with start_fuel litres and uses one litre per kilometre. stations[i] = [position, litres], sorted by position. Return the minimum number of refuelling stops to reach target kilometres, or -1 if it is impossible.

Examples

EXAMPLE WALKTHROUGHalready there

A guided view of the published example, not a trace of your code.

INPUT[1,1,[]]
TASKfewest_fuel_stops()
EXPECTED OUTPUT0

Start with the input values shown in this example.

Example 1 of 3

Input: target = 1, start_fuel = 1, stations = []

Output: 0

Explanation: already there

Input: target = 100, start_fuel = 1, stations = [[10,100]]

Output: -1

Explanation: can't reach

Input: target = 100, start_fuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]

Output: 2

Explanation: two stops

Hint 1

Drive as far as possible. Each time you can't reach the next point, refuel retroactively from the largest station you passed (a max-heap).

Topics

DSAHeapsPython