Practice › Heap & Priority Queue
Minimum Refueling Stops
Hard · Heap & Priority Queue · Asked at Amazon, Google, Microsoft
A car drives east from position 0 to target, using one litre of fuel per mile. It starts with startFuel litres in an unlimited tank. Gas station i is at pos[i] and has fuel[i] litres; stopping there takes all of it. Print the minimum number of stops needed to reach the target, or -1 if it is impossible. Arriving somewhere with 0 fuel left still counts as arriving.
Input
First line: target startFuel n. Next n lines: pos fuel (positions strictly increasing).
Output
The minimum number of stops, or -1.
Constraints
- 1 ≤ target, startFuel ≤ 10^9
- 0 ≤ n ≤ 10^4
- 0 < pos[i] < target
- 1 ≤ fuel[i] ≤ 10^9
Drive as far as the fuel allows, pushing every station you pass into a max-heap of fuel. When you run short, "retroactively" stop at the passed station with the most fuel (pop the max). Each pop is one stop. Use 64-bit integers.
Example 1
Input
1 1 0
Output
0
Example 2
Input
100 1 1 10 100
Output
-1
Example 3
Input
100 10 4 10 60 20 30 30 30 60 40
Output
2
Hints
- You do not have to decide at a station whether to stop there.
- Drive as far as possible; remember every station you passed.
- When you run short, pretend you had stopped at the passed station with the most fuel.
Approach
Let reach = how far the fuel taken so far can carry the car. While reach < target: push the fuel of every station with position ≤ reach into a max-heap; if the heap is empty, the target is unreachable (−1); otherwise pop the largest fuel, add it to reach and count one stop. Taking the biggest available tank first minimises the number of stops. Distances need 64 bits.
Complexity: O(n log n) time · O(n) space
Solution
C++ solution
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long target, reach;
int n;
cin >> target >> reach >> n;
vector<pair<long long, long long>> st(n);
for (auto &s : st) cin >> s.first >> s.second;
priority_queue<long long> pq;
int stops = 0, i = 0;
while (reach < target) {
while (i < n && st[i].first <= reach) pq.push(st[i++].second);
if (pq.empty()) {
cout << -1 << "\n";
return 0;
}
reach += pq.top();
pq.pop();
stops++;
}
cout << stops << "\n";
return 0;
}Python solution
import heapq
import sys
data = list(map(int, sys.stdin.read().split()))
target, reach, n = data[0], data[1], data[2]
stations = [(data[3 + 2 * i], data[4 + 2 * i]) for i in range(n)]
heap = []
stops = i = 0
answer = None
while reach < target:
while i < n and stations[i][0] <= reach:
heapq.heappush(heap, -stations[i][1])
i += 1
if not heap:
answer = -1
break
reach -= heapq.heappop(heap)
stops += 1
print(stops if answer is None else answer)Java solution
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
long target = (long) in.nval;
in.nextToken();
long reach = (long) in.nval;
in.nextToken();
int n = (int) in.nval;
long[][] st = new long[n][2];
for (int i = 0; i < n; i++) {
in.nextToken();
st[i][0] = (long) in.nval;
in.nextToken();
st[i][1] = (long) in.nval;
}
PriorityQueue<Long> pq = new PriorityQueue<>(Collections.reverseOrder());
int stops = 0, i = 0;
while (reach < target) {
while (i < n && st[i][0] <= reach) pq.add(st[i++][1]);
if (pq.isEmpty()) {
System.out.println(-1);
return;
}
reach += pq.poll();
stops++;
}
System.out.println(stops);
}
}Solve Minimum Refueling Stops online · All practice problems