Minimize Refueling Stops on a One-Direction Journey

Quick Overview

Minimize Refueling Stops on a One-Direction Journey. A car starts at coordinate0 with startFuel liters and travels east to target.

Minimize Refueling Stops on a One-Direction Journey

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem A car starts at coordinate0 with `startFuel` liters and travels east to `target`. It consumes one liter per mile and has an unlimited-capacity tank. Each station `[position,fuel]` offers all its fuel when you choose to stop there. Return the minimum number of refueling stops needed to reach target, or `-1` if impossible. Reaching a station or the destination with exactly zero fuel is allowed. ```cpp class Solution { public: int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations); }; ``` ### Examples **Example 1:** ```text Input: target = 50, startFuel = 10, stations = [[10,20],[25,25]] Output: 2 Explanation: The first stop extends reach to30; the second extends reach to55. One stop cannot reach50. ``` **Example 2:** ```text Input: target = 20, startFuel = 5, stations = [[6,20]] Output: -1 Explanation: The first station is one mile beyond the initial reach. ``` ### Constraints - Both target and the initial fuel are integers in the inclusive interval `[1,1000000000]`. - There may be zero stations; at most500 are supplied. - Station positions are integers at least1, strictly increasing, and strictly less than target. - Each station supplies an integer fuel amount satisfying `1 <= fuel_i < 1000000000`. ### Contract considerations All station positions are strictly before target. Transferring fuel can be modeled retrospectively because every heap station was already reachable. ```hint Read the boundary condition When forced to add one stop, which reachable station gives the greatest possible extension? ``` ### Boundary reminders All station positions are strictly before target. Transferring fuel can be modeled retrospectively because every heap station was already reachable.

Overview: Minimize Refueling Stops on a One-Direction Journey. A car starts at coordinate0 with startFuel liters and travels east to target.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

A car starts at coordinate0 with startFuel liters and travels east to target. It consumes one liter per mile and has an unlimited-capacity tank. Each station [position,fuel] offers all its fuel when you choose to stop there. Return the minimum number of refueling stops needed to reach target, or -1 if impossible. Reaching a station or the destination with exactly zero fuel is allowed.

class Solution {
public:
    int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations);
};

Examples

Example 1:

Input: target = 50, startFuel = 10, stations = [[10,20],[25,25]]
Output: 2
Explanation: The first stop extends reach to30; the second extends reach to55. One stop cannot reach50.

Example 2:

Input: target = 20, startFuel = 5, stations = [[6,20]]
Output: -1
Explanation: The first station is one mile beyond the initial reach.

Constraints

  • Both target and the initial fuel are integers in the inclusive interval [1,1000000000] .
  • There may be zero stations; at most500 are supplied.
  • Station positions are integers at least1, strictly increasing, and strictly less than target.
  • Each station supplies an integer fuel amount satisfying 1 <= fuel_i < 1000000000 .

Contract considerations

All station positions are strictly before target. Transferring fuel can be modeled retrospectively because every heap station was already reachable.

Boundary reminders

All station positions are strictly before target. Transferring fuel can be modeled retrospectively because every heap station was already reachable.